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

发布于 2024-08-22 12:16:09 字数 151 浏览 5 评论 0原文

尝试过这个,但仅适用于 UIButton:

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

Tried this but only works for UIButton:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

扭转时空 2024-08-29 12:16:09

只需直接设置 UIBarButtonItem 的 targetaction 属性即可。

Just set the UIBarButtonItem's target and action properties directly.

秋风の叶未落 2024-08-29 12:16:09

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

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

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);
}
记忆で 2024-08-29 12:16:09

设置 UIBarButtonItemtargetaction

Swift 5 & 4

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

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

Set target and action of your UIBarButtonItem

Swift 5 & 4

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

@objc func action (sender:UIButton) {
    print("action")
}
倒数 2024-08-29 12:16:09

我遇到了类似的问题...我假设您的意思是,如果您的 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:)];

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:

-btnClicked:(id)sender

to

-(void) btnClicked:(id)sender

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

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

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
如歌彻婉言 2024-08-29 12:16:09

如果您在代码中需要多次这样做,那么最好继续扩展 UIBarButtonItem ,我在下面用 Swift 完成了这一点。 :)

import UIKit

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

举个例子,使用 self 作为 UIViewController,您只需调用:

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

If you need this enough times in your code, it's nice to go ahead and extend UIBarButtonItem which I've done below in 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.myBarButtonItem.addTargetForAction(self, action: #selector(buttonPressed(_:))
蒗幽 2024-08-29 12:16:09
UIBarButtonItem *barListBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(getTruckStopListAction)];   
self.navigationItem.rightBarButtonItem = barListBtn;
[barListBtn release];
UIBarButtonItem *barListBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(getTruckStopListAction)];   
self.navigationItem.rightBarButtonItem = barListBtn;
[barListBtn release];
转身泪倾城 2024-08-29 12:16:09

对于自定义 UIBarButtonItem ,这是可行的。

let button = UIButton(type: .custom)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(yourFunction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)

您将目标和操作设置为要添加到 UIBarButtonItem 的按钮。

For a custom UIBarButtonItem this works.

let button = UIButton(type: .custom)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(yourFunction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)

You set the target and action to the button that you are adding to your UIBarButtonItem.

血之狂魔 2024-08-29 12:16:09

@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:)];

@wp42 It does work today.

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

.h file

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (addons)

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

@end

.m file

#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:)];
思念满溢 2024-08-29 12:16:09

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

profileImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileImageTap))
profileImageView.addGestureRecognizer(tap)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileImageView)

For custom views: use an UITapGestureRecognizer and set up isUserInteractionEnabled to true.

profileImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileImageTap))
profileImageView.addGestureRecognizer(tap)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileImageView)
禾厶谷欠 2024-08-29 12:16:09

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

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 *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)#>
二手情话 2024-08-29 12:16:09

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

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

Swift 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.

樱花坊 2024-08-29 12:16:09

您可能想尝试 addTarget 方法。

You may want to try out the addTarget method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文