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?
This question is related to
ios
uiviewcontroller
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.
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)
Reinitialise the view controller
YourViewController *vc = [[YourViewController alloc] initWithNibName:@"YourViewControllerIpad" bundle:nil];
[self.navigationController vc animated:NO];
You Must use
-(void)viewWillAppear:(BOOL)animated
and set your entries like you want...
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().
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)
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.
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
}
}
Update the data ... change button titles..whatever stuff you have to update..
then just call
[self.view setNeedsDisplay];
Source: Stackoverflow.com