gpt4 book ai didi

objective-c - 在右栏按钮项的选择器中传递参数

转载 作者:行者123 更新时间:2023-11-28 18:06:48 24 4
gpt4 key购买 nike

我有这个代码:

UIBarButtonItem *donebutton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];
self.navigationItem.rightBarButtonItem = donebutton;

现在我想将一些参数传递给该方法:

- (void)doneButtonPressed{}

怎么做?

最佳答案

如果你想传递对象说字符串然后使用setAssociatedObject:

首先添加

#import <objc/runtime.h>

现在

NSString *strText = @"text";

objc_setAssociatedObject(donebutton, "Argument", strText, OBJC_ASSOCIATION_RETAIN_NONATOMIC); // provide button , key , object for passing

然后在你需要参数的地方像这样检索:

NSString *str = objc_getAssociatedObject(donebutton, "Argument"); //using button and key
//remove object associated for button if not needed.

但是如果你想要按钮引用那么

- (void)doneButtonPressed:(id)sender{
UIButton *btnClicked = (UIButton *)sender;
.......
}

关于objective-c - 在右栏按钮项的选择器中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com