如何从视图动态隐藏按钮? [英] How can I dynamically hide a button from a view?
问题描述
我想根据某个条件动态隐藏我的某个视图中的按钮。
I would like to dynamically hide a button in one of my views depending on a certain condition.
我试图向视图控制器的 -viewWillAppear
方法,在显示实际视图之前隐藏按钮,但我仍然不知道如何做。
I tried adding some code to the view controller's -viewWillAppear
method, to make the button hidden before displaying the actual view, but I still don't know how to do that.
我有一个通过IBOutlet的按钮的引用,但我不知道如何从这里向前移动。作为参考,这是一个UIBarButtonItem实例。
I have a reference to the button through an IBOutlet, but I'm not sure how to move forward from here. For reference, this is a UIBarButtonItem instance.
推荐答案
如果你想隐藏一个UIBarButtonItem,修改父栏的内容。如果是UIToolBar,你需要将bar的items数组设置为不包含你的项目的数组。
If you're trying to hide a UIBarButtonItem, you'll actually have to modify the contents of the parent bar. If it's a UIToolBar, you'll need to set the bar's items array to an array that doesn't include your item.
NSMutableArray *items = [[myToolbar.items mutableCopy] autorelease];
[items removeObject: myButton];
myToolbar.items = items;
这篇关于如何从视图动态隐藏按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!