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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2333638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:58:31  来源:igfitidea点击:

How to set target and action for UIBarButtonItem at runtime

iosobjective-ciphoneswiftuibarbuttonitem

提问by Sharief

Tried this but only works for UIButton:

试过这个,但只适用于 UIButton:

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

回答by Ole Begemann

Just set the UIBarButtonItem's targetand actionproperties directly.

只需直接设置 UIBarButtonItemtargetaction属性。

回答by mihai

UIBarButtonItem doesnt have the same addTarget method so you have to set them directly as follows

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

回答by Haroldo Gondim

Set targetand actionof your UIBarButtonItem

设置targetaction你的UIBarButtonItem

Swift 5 & 4

斯威夫特 5 & 4

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

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

回答by J. Dave

I ran into a similar problem... I assume you mean that if your UIButton is not part of your UITabBar to call btnClicked then it works appropriately. If this is the problem you are proposing then, check your btnClicked method and change it from:

我遇到了类似的问题......我假设你的意思是如果你的 UIButton 不是你的 UITabBar 的一部分来调用 btnClicked 那么它可以正常工作。如果这是您提出的问题,请检查您的 btnClicked 方法并将其更改为:

-btnClicked:(id)sender

to

-(void) btnClicked:(id)sender

that, and declare btnClicked in the header file...

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

For what it's worth, this is how I setup a button in tabbarbuttonitem:

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

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

回答by serenn

If you need this enough times in your code, it's nice to go ahead and extend UIBarButtonItemwhich I've done below in Swift. :)

如果您在代码中需要足够多的时间,那么继续并扩展UIBarButtonItem我在下面在 Swift 中完成的内容会很好。:)

import UIKit

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

As an example, with self as a UIViewController, you'd simply call:

例如,将 self 作为 a UIViewController,您只需调用:

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

回答by sinh99

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

回答by Itai Spector

@wp42 It does work today.

@ wp42 今天确实有效。

A nifty way of doing this in objective-C is adding a category to UIBarButtonItem class:

在 Objective-C 中这样做的一个好方法是向 UIBarButtonItem 类添加一个类别:

.h file

.h 文件

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (addons)

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

@end

.m file

.m 文件

#import "UIBarButtonItem+addons.h"

@implementation UIBarButtonItem (addons)

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

@end

In practice:

在实践中:

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

回答by Adam Cooper

If you are programmatically adding the UIBarButtonItem, the best way to set the target and action is to initialize the button with one of the following methods:

如果您以编程方式添加 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)#>

回答by atereshkov

Swift 5:

斯威夫特 5:

Extract to extensions:

提取到扩展名:

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

}

Call in code:

调用代码:

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

Action:

行动:

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

Pretty easy to add new buttons to this factory.

向这个工厂添加新按钮非常容易。

回答by David Carvalho

You may want to try out the addTarget method.

您可能想尝试 addTarget 方法。