由于UIBarButton
不是继承自UIResponder/UIControl
,点击事件如何UIBarButton
工作?
user4886069
问问题
4811 次
2 回答
3
只需直接创建 UIBarButtonItem 的
target
和action
属性。
UIBarButtonItem *barListBtn = [[UIBarButtonItem alloc] initWithTitle:@"yourTitle"
style:UIBarButtonItemStylePlain
target:self action:@selector(btnClicked:)];
self.navigationItem.rightBarButtonItem = barListBtn;
-(void)btnClicked:(UIBarButtonItem*)btn
{
NSLog(@"button tapped %@", btn.title);
}
选择-2
- (void) viewDidLoad
{
[super viewDidLoad];
// first we create a button and set it's properties
UIBarButtonItem *myButton = [[UIBarButtonItem alloc]init];
myButton.action = @selector(doTheThing);
myButton.title = @"Hello";
myButton.target = self;
// then we add the button to the navigation bar
self.navigationItem.rightBarButtonItem = myButton;
}
// method called via selector
- (void) doTheThing {
NSLog(@"Doing the thing");
}
一些额外的样本
于 2016-02-15T07:02:29.487 回答
0
UIBarItem
是添加到出现在屏幕底部的条的项目的抽象超类。条上的项目的行为类似于按钮(的实例UIButton
)。它们有标题、图像、动作和目标。您还可以启用和禁用栏上的项目。
有关更多详细信息,请查看下面提到的链接: https ://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarItem_Class/index.html#//apple_ref/occ/cl/UIBarItem
斯威夫特:
添加以下行viewDidLoad
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:"), animated: true)
功能:
func barButtonItemClicked()
{
print("Bar button clicked")
}
于 2016-02-15T07:11:07.580 回答