我有一个View控制器显示一些信息(而不是表视图)。
我对远程服务器进行了更新调用,该服务器填充了我的数据库。我想在更新调用完成后重新加载我的ViewController。
我该怎么办?
答案 0 :(得分:24)
如果您知道您的数据库已经更新,并且您只想刷新viewcontrolle(这也是我的情况)。我没有找到任何其他解决方案,但我做的是 当我的数据库更新时,我称之为
[self viewDidLoad];
再次,它起作用了。 请记住,如果您覆盖其他viewWillAppear或loadView,则以相同的顺序调用它们。 等。
[self viewDidLoad]; [self viewWillAppear:YES];
但我认为应该有一个更具体的解决方案,如浏览器中的刷新按钮。 如果有人有plz上传anse
答案 1 :(得分:16)
你真的不需要这样做:
[self.view setNeedsDisplay];
老实说,我认为“让我们希望找到最好的”解决方案,在这种情况下 。有几种方法可以更新UIView
:
每个人都有利有弊。 根据您要更新的内容以及业务层(服务器连接)与UIViewController
之间的“连接”类型,我可以推荐一个适合您需求的方式。 < / p>
答案 2 :(得分:11)
如果要重新加载最初从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
}
}
答案 3 :(得分:5)
试试这个,你必须调用两个方法来重新加载视图, 因为这个适合我,
-Objective-C
[self viewDidLoad];
[self viewWillAppear:YES];
-Swift
self.viewDidLoad()
self.viewWillAppear(true)
答案 4 :(得分:2)
更新数据...更改按钮标题..无论你需要更新什么东西..
然后打电话
[self.view setNeedsDisplay];
答案 5 :(得分:1)
再次指向您的ViewController。在我的情况[self.view setNeedsDisplay];
和[self viewDidLoad]; [self viewWillAppear:YES];
不起作用,但下面的方法有效。
在目标C
UIStoryboard *MyStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil ];
UIViewController *vc = [MyStoryboard instantiateViewControllerWithIdentifier:@"ViewControllerStoryBoardID"];
[self presentViewController:vc animated:YES completion:nil];
夫特:
let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ViewControllerStoryBoardID")
self.presentViewController(secondViewController, animated: true, completion: nil)
答案 6 :(得分:1)
不建议您自己调用ViewDidLoad或ViewWillAppear。
在ViewDidLoad中包含一个loadData()函数来准备数据。这是在初始运行中执行的。
如果要重新加载,请再次调用loadData()以从模型中获取数据。在tableView中调用reloadData()或在常规视图中调用setNeedsDisplay()。
答案 7 :(得分:0)
重新初始化视图控制器
YourViewController *vc = [[YourViewController alloc] initWithNibName:@"YourViewControllerIpad" bundle:nil];
[self.navigationController vc animated:NO];
答案 8 :(得分:0)
对于UIViewController
,只需再次加载视图-
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)
}
}
我正在将我的视图控制器加载到概要文件编辑和视图概要文件中。根据{{1}}的Bool值,以isEditProfile
方法更新视图。
答案 9 :(得分:-6)
您必须使用
-(void)viewWillAppear:(BOOL)animated
并设置您想要的条目......