Reloading a ViewController

2019-03-10 15:07发布

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?

9条回答
放我归山
2楼-- · 2019-03-10 15:10

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)
查看更多
闹够了就滚
3楼-- · 2019-03-10 15:11

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

then just call

[self.view setNeedsDisplay];
查看更多
Summer. ? 凉城
4楼-- · 2019-03-10 15:15

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)
查看更多
Luminary・发光体
5楼-- · 2019-03-10 15:20

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 
    }
}
查看更多
Deceive 欺骗
6楼-- · 2019-03-10 15:21

if you know your database hasbee updated and you want to just refresh viewcontrolle(which is mine case too). i didnt find anyother solution but what i did is 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];

but i think there should be a more specific solution like refresh button in browser. if anyone have plz upload anse

查看更多
Summer. ? 凉城
7楼-- · 2019-03-10 15:28

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

查看更多
登录 后发表回答