简体   繁体   English

如何使用UIButtons更新视图

[英]How to update view with UIButtons

I have buttons in my navigation bar and in a toolbar subview of a container view whose enabled property depends on app state. 我的导航栏和容器视图的工具栏子视图中都有按钮,其enabled属性取决于应用程序状态。 Currently the buttons are set in viewWillAppear , so the right thing happens if I navigate away from the view then return. 当前,按钮是在viewWillAppear中设置的,所以如果我离开视图然后返回,则正确的事情发生了。 I thought [containerView setNeedsDisplay] would do the trick, but no. 我以为[containerView setNeedsDisplay]可以解决问题,但没有。

(I have UITextViews in the container view that I can force to update when textView.text is changed, but the app logic is such that it is harder to explicitly update all of the correct buttons as the state changes.) (我在容器视图中有UITextViews,当textView.text更改时,我可以强制更新它,但是应用程序逻辑使得当状态更改时,很难显式地更新所有正确的按钮。)


You can change the state of navigationBar and toolbar buttons anywhere in your code. 您可以在代码中的任何位置更改navigationBar和工具栏按钮的状态。 For initial setup of the view, it's useful to put some code in the viewWillDisappear and viewWillAppear, as you have done. 对于视图的初始设置,像完成操作一样,将一些代码放入viewWillDisappear和viewWillAppear很有用。

For example, if you wanted to programatically change a button on the navigationBar from 'Cancel' to 'Enter' when you've added some text to your textField you can just initialise a new UIBarButtonItem and stick it where the old one was. 例如,如果您想在向textField添加一些文本后以编程方式将navigationBar上的按钮从“取消”更改为“ Enter”,则只需初始化一个新的UIBarButtonItem并将其粘贴在旧的位置即可。 Say you added the 'Cancel' button in your viewWillAppear like so... 假设您在viewWillAppear中添加了“取消”按钮,如下所示:

- (void)viewWillAppear:(BOOL)animated {
self.navigationItem.rightBarButtonItem = 
[[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)] autorelease];
}

then elsewhere in your code you can simply overwrite that navigation item by making a new one in exactly the same way. 然后,您可以在代码中的其他位置以完全相同的方式制作一个新的导航项,从而简单地覆盖该导航项。 However, you're clearly going to want to give it a different title ('Enter' in my example here) and perhaps a different action. 但是,您显然会想给它一个不同的标题(在我的示例中为“ Enter”),也许还有一个不同的动作。


In terms of the toolbar, UINavigationControllers can easily handle changes. 就工具栏而言,UINavigationControllers可以轻松处理更改。 This is because every UINavigationController+UIViewController combo come with a built in toolbar, it's just that they don't display it unless you specifically request that. 这是因为每个UINavigationController + UIViewController组合都带有内置的工具栏,只是除非您特别要求,否则它们不会显示它。 The best method to do so is to set the toolbar items for the UIViewController and then ask the navigationController to display/hide the toolbar as required. 最好的方法是设置UIViewController的工具栏项,然后要求navigationController根据需要显示/隐藏工具栏。

So, for example, say I make three UIBarButtonItem instances in the initialisation method of the UIViewController... then all I need to do is stick them in an array and assign that array of items to the toolbar, eg 因此,例如,假设我在UIViewController的初始化方法中创建了三个UIBarButtonItem实例...那么我要做的就是将它们粘贴在一个数组中,然后将该项目数组分配给工具栏,例如

[self setToolbarItems:[NSArray arrayWithObjects:button1, button2, button3, nil] animated:NO];

then in the viewWillAppear (or viewDidAppear) methods I can use the navigationController to display the toolbar: 然后在viewWillAppear(或viewDidAppear)方法中,我可以使用navigationController来显示工具栏:

[self.navigationController setToolbarHidden:NO animated:YES];

Just remember to do the opposite when the view will go away (ie the same call but with setToolbarHidden:YES). 只要记得在视图消失时执行相反的操作即可(即,相同的调用,但使用setToolbarHidden:YES)。 At any point in the code for your UIViewController, you can change the buttons using the same approach as you did in the initialisation, ie [self setToolbarItems:.....]. 在UIViewController的代码中的任何时候,都可以使用与初始化相同的方法来更改按钮,即[self setToolbarItems:.....]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM