45

I have a View controller displaying some information (not table views).

I have an update call to a remote server which fills my data base. I would like to completely reload my ViewController after the update call is done.

What should I do?

4
  • 2
    How do you currently update your data - I am assuming you are using viewDidAppear? Commented Mar 13, 2012 at 14:53
  • Can't you use either the NotificationCenter or a protocol to inform your ViewController that the update has ended? Commented Mar 13, 2012 at 14:55
  • @Khal I've never used Notification center, could you point me to a tutorial or guide? @Till I use viewWill/DidAppear as well as loadView and viewDidLoad. Commented Mar 13, 2012 at 15:06
  • Here is a tutorial on NSNotificationCenter : blog.shoguniphicus.com/2011/03/07/… But just for your own record, I would rather use a protocol to inform your ViewController that the update has ended ;) Commented Mar 14, 2012 at 12:14

10 Answers 10

28

If you know your database has been updated and you want to just refresh your ViewController (which was my case). I didn't find another solution but what I did was when my database updated, I called:

[self viewDidLoad];

again, and it worked. Remember if you override other viewWillAppear or loadView then call them too in same order. like.

[self viewDidLoad]; [self viewWillAppear:YES];

I think there should be a more specific solution like refresh button in browser.

Sign up to request clarification or add additional context in comments.

1 Comment

Never ever call lifecycle methods !!! use [self.view setNeedsDisplay] or [self.view setNeedsLayout] for example
17

If you want to reload a ViewController initially loaded from a XIB, you can use the next UIViewController extension:

extension UIViewController {
    func reloadViewFromNib() {
        let parent = view.superview
        view.removeFromSuperview()
        view = nil
        parent?.addSubview(view) // This line causes the view to be reloaded 
    }
}

1 Comment

Hi @juancazalla, I tried the above snippet for refreshing my view controller, so Its again calling viewdidload() method from controller. how to escape this view did load call and refresh my view.
9

You really don't need to do:

[self.view setNeedsDisplay];

Honestly, I think it's "let's hope for the best" type of solution, in this case. There are several approaches to update your UIViews:

  1. KVO
  2. Notifications
  3. Delegation

Each one has is pros and cons. Depending of what you are updating and what kind of "connection" you have between your business layer (the server connectivity) and the UIViewController, I can recommend one that would suit your needs.

3 Comments

I went with this one when I needed it.
This answer doesn't really answer the question. You suggest 3 ways to reload the view, but you don't elaborate or even provide links to information on how to implement any of those suggestions.
I want to reload a subview with animation similar to pushing a new view.
8

Try this, You have to call both methods to reload view, as this one works for me,

-Objective-C

[self viewDidLoad]; 
[self viewWillAppear:YES];

-Swift

self.viewDidLoad()
self.viewWillAppear(true)

4 Comments

This is not right answer. When u call viewDidLoad for reload view then memory will increase for your application. It's not proper answer..
still it works for other so please dont make downmark @Singapore
Calling controller lifecycle methods is not at all recommended
so is there any way to reload without increasing memory of the application ? @Singapore
2

Direct to your ViewController again. in my situation [self.view setNeedsDisplay]; and [self viewDidLoad]; [self viewWillAppear:YES];does not work, but the method below worked.

In objective C

UIStoryboard *MyStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil ];
UIViewController *vc = [MyStoryboard instantiateViewControllerWithIdentifier:@"ViewControllerStoryBoardID"];
[self presentViewController:vc animated:YES completion:nil];

Swift:

let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ViewControllerStoryBoardID")   
self.presentViewController(secondViewController, animated: true, completion: nil)

2 Comments

In my view, the best way to "reload the ViewController" is simply to remove the object from memory and create an entirely new one (as is done above). This also removes viewModels, etc. from memory that were instantiated in the VC (assuming no circular reference issues). Just switching out views doesn't really cut it, and doesn't account for logic running outside the VC that's being refreshed.
'presentViewController(:animated:completion:)' has been renamed to 'present(:animated:completion:)': swift let secondViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewControllerStoryBoardID") self.present(secondViewController, animated: true, completion: nil)
1

Update the data ... change button titles..whatever stuff you have to update..

then just call

[self.view setNeedsDisplay];

1 Comment

I have to reload a couple of entries from the Database. I don't think that redrawing the views is the way to go here.
1

It is not advised to call ViewDidLoad or ViewWillAppear by yourself.

In the ViewDidLoad include a loadData() function to prepare the data. This is executed in the initial run.

When you want to reload, call loadData() again to get the data from model. In a tableView call reloadData() or in a regular view setNeedsDisplay().

Comments

0

Reinitialise the view controller

YourViewController *vc = [[YourViewController alloc] initWithNibName:@"YourViewControllerIpad" bundle:nil];
[self.navigationController vc animated:NO];

1 Comment

this is old-style. Modern projects with storyboard most likely won't use XIB; use Storyboard instead.
-1

For UIViewController just load your view again -

func rightButtonAction() {
        if isEditProfile {
           print("Submit Clicked, Call Update profile API")
            isEditProfile = false
            self.viewWillAppear(true)
        } else {
            print("Edit Clicked, Call Edit profile API")
            isEditProfile = true
            self.viewWillAppear(true)
        }
    }

I am loading my view controller on profile edit and view profile. According to the Bool value isEditProfile updating the view in viewWillAppear method.

Comments

-6

You Must use

-(void)viewWillAppear:(BOOL)animated

and set your entries like you want...

1 Comment

This might be a valid hint for for when to send the request to a web service to load data, but it doesn't answer how to handle the web service response asynchronously to update the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.