ios 重新加载一个 ViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9686433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:17:03  来源:igfitidea点击:

Reloading a ViewController

iosuiviewcontroller

提问by Tiago Veloso

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.

我有一个远程服务器的更新调用,它填满了我的数据库。我想在更新调用完成后完全重新加载我的 ViewController。

What should I do?

我该怎么办?

采纳答案by Rui Peres

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:

老实说,我认为这是一种解决方案,“我们希望最好的”在这种情况下。有几种方法可以更新您的UIViews:

  1. KVO
  2. Notifications
  3. Delegation
  1. KVO
  2. 通知
  3. 代表团

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.

每一种都有利有弊。根据您正在更新的内容以及您的业务层(服务器连接)与UIViewController.

回答by Moaz Saeed

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:

如果您知道您的数据库已更新并且您只想刷新您的 ViewController(这是我的情况)。我没有找到其他解决方案,但我所做的是在我的数据库更新时,我调用了:

[self viewDidLoad];

[self viewDidLoad];

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

再次,它奏效了。请记住,如果您覆盖其他 viewWillAppear 或 loadView,则也以相同的顺序调用它们。喜欢。

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

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

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

我认为应该有一个更具体的解决方案,比如浏览器中的刷新按钮。

回答by juancazalla

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

如果要重新加载最初从 XIB 加载的 ViewController,可以使用下一个 UIViewController 扩展:

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

回答by Patel Jigar

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

试试这个,你必须调用这两种方法来重新加载视图,因为这个对我有用,

-Objective-C

-目标-C

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

-Swift

-迅速

self.viewDidLoad()
self.viewWillAppear(true)

回答by Shubhank

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

更新数据……更改按钮标题……无论您需要更新什么……

then just call

然后就打电话

[self.view setNeedsDisplay];

回答by Shubhank

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.

再次指向您的 ViewController。在我的情况下 [self.view setNeedsDisplay];[self viewDidLoad]; [self viewWillAppear:YES];不起作用,但下面的方法有效。

In objective C

在目标 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)

回答by Vincent

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

不建议自行调用 ViewDidLoad 或 ViewWillAppear。

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

在 ViewDidLoad 中包含一个 loadData() 函数来准备数据。这是在初始运行时执行的。

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

当你想重新加载时,再次调用 loadData() 从模型中获取数据。在 tableView 调用 reloadData() 或在常规视图 setNeedsDisplay() 中。

回答by Muhammad Aamir Ali

Reinitialise the view controller

重新初始化视图控制器

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

回答by plop

You Must use

你必须使用

-(void)viewWillAppear:(BOOL)animated

and set your entries like you want...

并根据需要设置您的条目...