如何在运行时为UIBarButtonItem设置目标和操作

时间:2010-02-25 11:36:44

标签: ios objective-c iphone swift uibarbuttonitem

试过这个但只适用于UIButton:

[btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

11 个答案:

答案 0 :(得分:96)

直接设置UIBarButtonItem的targetaction属性。

答案 1 :(得分:20)

UIBarButtonItem没有相同的addTarget方法,所以你必须直接设置它们如下

btn.target = self;
btn.action = @selector(barButtonCustomPressed:);

...

// can specify UIBarButtonItem instead of id for this case
-(IBAction)barButtonCustomPressed:(UIBarButtonItem*)btn 
{
    NSLog(@"button tapped %@", btn.title);
}

答案 2 :(得分:14)

我遇到了类似的问题...我认为你的意思是如果你的UIButton不是你的UITabBar的一部分来调用btnClicked然后它适当地工作。如果这是您提出的问题,请检查您的btnClicked方法并将其更改为:

-btnClicked:(id)sender

-(void) btnClicked:(id)sender

那,并在头文件中声明btnClicked ......

对于它的价值,这就是我在tabbarbuttonitem中设置按钮的方式:

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];

答案 3 :(得分:9)

如果您在代码中需要足够的时间,那么继续前进并扩展我在Swift中完成的UIBarButtonItem是很好的。 :)

import UIKit

extension UIBarButtonItem {
    func addTargetForAction(target: AnyObject, action: Selector) {
        self.target = target
        self.action = action
    }
}

例如,将自己设为UIViewController,您只需致电:

self.myBarButtonItem.addTargetForAction(self, action: #selector(buttonPressed(_:))

答案 4 :(得分:9)

设置target

actionUIBarButtonItem

Swift 5& 4

button.target = self
button.action = #selector(action)

@objc func action (sender:UIButton) {
    print("action")
}

答案 5 :(得分:7)

  UIBarButtonItem *barListBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(getTruckStopListAction)];   
    self.navigationItem.rightBarButtonItem = barListBtn;
    [barListBtn release];

答案 6 :(得分:1)

@ wp42今天确实有用。

在Objective-C中执行此操作的一种巧妙方法是向UIBarButtonItem类添加一个类别:

.h文件

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (addons)

-(void)addTarget:(id)target andAction:(SEL)action;

@end

.m文件

#import "UIBarButtonItem+addons.h"

@implementation UIBarButtonItem (addons)

-(void)addTarget:(id)target andAction:(SEL)action{
   [self setTarget:target];
   [self setAction:action];
}

@end

在实践中:

[myBtn addTarget:self andAction:@selector(myFunction:)];

答案 7 :(得分:0)

如果以编程方式添加UIBarButtonItem,设置目标和操作的最佳方法是使用以下方法之一初始化按钮:

UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithImage:<#(UIImage)#> style:<#(UIBarButtonItemStyle)#> target:<#(id)#> action:<#(SEL)#>

UIBarButtonItem *customButton = [UIBarButtonItem alloc] initWithTitle:<#(NSString *)#> style:<#(UIBarButtonItemStyle)#> target:<#(id)#> action:<#(SEL)#>

UIBarButtonItem *customButton = [UIBarButtonItem alloc] initWithImage:<#(UIImage *)#> landscapeImagePhone:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(id)#> action:<#(SEL)#>

答案 8 :(得分:0)

快捷键5:

提取为扩展程序:

extension UIBarButtonItem {

    static func nextBtn(target: AnyObject, action: Selector) -> UIBarButtonItem {
        let title = "Next"
        return button(title: title, target: target, action: action)
    }

    private static func button(title: String, target: AnyObject, action: Selector) -> UIBarButtonItem {
        return UIBarButtonItem(title: title, style: .done, target: target, action: action)
    }

}

输入代码:

navigationItem.rightBarButtonItem = .nextBtn(target: self, action: #selector(rightBarButtonAction))

操作:

@objc func rightBarButtonAction() {
    Swift.print("Button tapped!")
}

添加新按钮非常容易。

答案 9 :(得分:0)

对于自定义视图:使用UITapGestureRecognizer并将isUserInteractionEnabled设置为true。

    profileImageView.isUserInteractionEnabled = true

    let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileImageTap))
    profileImageView.addGestureRecognizer(tap)

    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileImageView)

答案 10 :(得分:-5)

您可能想尝试使用addTarget方法。