Tiago Veloso
Tiago Veloso

Reputation: 8563

Reloading a ViewController

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?

Upvotes: 45

Views: 142157

Answers (10)

Shashi Tiwari
Shashi Tiwari

Reputation: 65

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.

Upvotes: -1

Moaz Saeed
Moaz Saeed

Reputation: 1016

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.

Upvotes: 28

Vincent
Vincent

Reputation: 4409

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().

Upvotes: 1

juancazalla
juancazalla

Reputation: 1090

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 
    }
}

Upvotes: 17

user4410354
user4410354

Reputation:

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)

Upvotes: 2

Patel Jigar
Patel Jigar

Reputation: 2151

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)

Upvotes: 8

Rui Peres
Rui Peres

Reputation: 25907

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.

Upvotes: 9

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21087

Reinitialise the view controller

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

Upvotes: 0

plop
plop

Reputation: 11

You Must use

-(void)viewWillAppear:(BOOL)animated

and set your entries like you want...

Upvotes: -6

Shubhank
Shubhank

Reputation: 21805

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

then just call

[self.view setNeedsDisplay];

Upvotes: 1

Related Questions