【问题标题】:How do the click events on UIBarButtonUIBarButton上的点击事件怎么做
【发布时间】:2016-02-15 06:52:12
【问题描述】:

由于UIBarButton 不是从UIResponder/UIControl 继承的,UIBarButton 上的点击事件如何工作?

【问题讨论】:

    标签: ios swift uibarbuttonitem uicontrol uicontrolevents


    【解决方案1】:

    只需直接创建 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);
    }
    

    选择 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");
    
    }
    

    一些额外的Sample

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-25
        • 1970-01-01
        相关资源
        最近更新 更多