简体   繁体   English

如何在UIBarButton上点击事件

[英]How do the click events on UIBarButton

由于UIBarButton未从继承UIResponder/UIControl ,如何在点击事件UIBarButton工作?

Just create the UIBarButtonItem's target and action properties directly. 只需直接创建UIBarButtonItem的targetaction属性即可。

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");

}

some additional Sample 一些额外的样本

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.

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