Why doesn't my view reload?

陌路散爱 提交于 2019-12-23 05:39:11

问题


So I have two search bars, a couple of buttons and a tableView below those (yes, all these) in one view. And I also have a navigation bar on top of it all with a back button.

For a particular operation, I remove all search bars, buttons and I display only one uitableviewcell in my view.

If I press edit, I want the whole view to be reloaded, not just the tableView but I want the view to have the search bars and buttons and the navigation screen.

I did [self.view reloadInputViews] in the IBAction of the edit button. Control goes here, but the view is just not reloaded. Why?


回答1:


reloadInputViews is specifically used for views that are firstResponders. Probably self.view is not the first responder at that time. But why you want to do all this using "reloadInputViews", it wouldn't be easier to use: [self.view setNeedsLayout] ?




回答2:


A better way to handle refreshing the state of a view is to abstract the configuration of your UIViewController's view's subviews into a custom method that you can call initially from viewDidLoad and again as an action of your UIbarButtonItem.

- (void)viewDidLoad {
    //Specific, non repeated view setup
    [self resetViewState];
}

- (void)resetViewState {
    //Repeatable view setup
}

The reason it's worth making the distinction is because viewDidLoad is called automatically by the system after the view is lazy loaded. By design it's not a method that should be called multiple times per view. It will be called again if the view is discarded and recreated.

You can also move the layout of your UIViewController's subviews into viewWillLayoutSubviews if you need to do layout work, again this gets called by the system at various times.




回答3:


I do not know how I get the answer myself right after posting it here on StackOverflow. Sorry for the trouble guys (sigh!)

I did [self viewDidLoad];



来源:https://stackoverflow.com/questions/6539784/why-doesnt-my-view-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!