UIToolBar-禁用按钮 [英] UIToolBar - disable buttons
本文介绍了UIToolBar-禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我有一个工具栏,在某个时候我想禁用或启用某些按钮.这样做最简单的方法是什么?如何访问UIToolbar的items属性?
in my app I have a toolbar and at a certain point I want to disable or enable some buttons. What is the easiest way to do so? How can I access items property of UIToolbar?
这是我的代码:
-(void)addToolbar {
NSMutableArray *buttons = [[NSMutableArray alloc] init];
//Define space
UIBarButtonItem *flexibleSpaceItem;
flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
//Add "new" button
UIBarButtonItem *newButton = [[UIBarButtonItem alloc]
initWithTitle:@"New" style:UIBarButtonItemStyleBordered target:self action:@selector(new_clicked)];
[buttons addObject:newButton];
[newButton release];
//Add space
[buttons addObject:flexibleSpaceItem];
//Add "make active" button
UIBarButtonItem *activeButton = [[UIBarButtonItem alloc]
initWithTitle:@"Make Active" style:UIBarButtonItemStyleBordered target:self action:@selector(make_active_clicked)];
[buttons addObject:activeButton];
[activeButton release];
[buttons addObject:flexibleSpaceItem];
//Add "edit" button
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(edit_clicked)];
[buttons addObject:editButton];
[editButton release];
[flexibleSpaceItem release];
[toolBar setItems:buttons];
[buttons release];
}
谢谢.
推荐答案
最简单的方法是将对UIBarButtonItem
的引用保存为实例变量.
The simplest way is to save a reference to the UIBarButtonItem
as an instance variable.
# header file
UIBarButtonItem *editButton;
然后您的代码变为
# .m file
editButton = [[UIBarButtonItem alloc]
initWithTitle:@"Edit"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(edit_clicked)];
[buttons addObject:editbutton];
现在在任何实例方法中的任何地方,禁用按钮都非常简单:
Now anywhere in any instance method, disabling the button is as simple as:
editButton.enabled = NO;
也不要立即release
它,因为该类现在拥有按钮对象.因此,在dealloc
方法中将其替换为release
.
Also dont release
it immediately, since this class now owns the button object. So release
it in the dealloc
method instead.
这篇关于UIToolBar-禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文