iphone - 如何将参数传递给此函数?

标签 iphone objective-c

我有以下代码:

[replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];

- (void)replyAll:(NSInteger)tid {
// some code
}

如何向 ReplyAll 函数发送参数?

最佳答案

replyAll 方法应该接受 (id)sender。如果 UIButton 触发了事件,那么相同的 UIButton 将作为发送者传递。 UIButton 有一个属性“标签”,您可以将自己的自定义数据附加到该属性(很像 .net winforms)。

因此,您可以将您的事件与:

[replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];
replyAllBtn.tag=15;

然后处理它:

(void) ReplyAll:(id)sender{
    NSInteger *tid = ((UIControl*)sender).tag;
    //...

关于iphone - 如何将参数传递给此函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/450222/

相关文章:

ios - 代码中的假平移手势

ios - 使用 touchesBegan Action 子类化 UIBarButtonItem

ios - 不写 init 方法就无法调用不同类中的方法

iphone - 一个窗口中的多个 View

ios - 无法在屏幕上绘制矩形 ios

ios - 调整导航栏中 UISearchBar 的大小?

objective-c - 在按钮上单击 "unrecognized selector sent to instance"

objective-c - ARC 和自定义 deallocs

iphone - 如何从 Facebook 检索照片

iphone - 如何为 UITable 中的移动行制作自定义按钮和单元格阴影?