[英]How do the click events on UIBarButton
由于UIBarButton
未从继承UIResponder/UIControl
,如何在点击事件UIBarButton
工作?
Just create the UIBarButtonItem's
target
andaction
properties directly.只需直接创建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);
}
Choice-2 选择-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");
}
UIBarItem
is an abstract superclass for items added to a bar that appears at the bottom of the screen. UIBarItem
是一个抽象超类,用于添加到显示在屏幕底部的栏的项目。 Items on a bar behave in a way similar to buttons (instances of UIButton
). 条形图上的项目的行为类似于按钮(
UIButton
实例)。 They have a title, image, action, and target . 他们有标题,图像,动作和目标 。 You can also enable and disable an item on a bar.
您还可以启用和禁用栏上的项目。
For more details checkout the link mentioned below: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarItem_Class/index.html#//apple_ref/occ/cl/UIBarItem 有关详细信息,请查看以下链接: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarItem_Class/index.html#//apple_ref/occ/cl/UIBarItem
Swift: Add the below line in viewDidLoad
Swift:在
viewDidLoad
添加以下行
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:"), animated: true)
Function: 功能:
func barButtonItemClicked()
{
print("Bar button clicked")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.