我正在尝试执行以下任务:
我知道我们可以使用按钮点击按钮方法:
[button addTarget:self action:@selector(performModalActionForButton)
forControlEvents:UIControlEventTouchUpInside];
方法就像:
(void) performModalActionForButton:(NSString *)str
{
}
我想在按钮单击上发送一个字符串到一个方法。我怎样才能做到这一点?
由于
答案 0 :(得分:5)
你的代码中有两个小错误。
如果你使用@selector
并希望选择带参数的函数,你可以添加:...所以它是@selector(performModalActionForButton:)
传递给函数的对象始终是消息的发送者..所以函数将是:
- (void) performModalActionForButton:(UIButton *)button
{
}
在该功能中,您可以检查按钮上的文本,然后检查...或标签或任何您想要的内容。
答案 1 :(得分:0)
按钮单击触发一个操作方法,然后发送其参数为您要发送的字符串的消息:
[button addTarget:self action:@selector(performModalActionForButton:)
forControlEvents:UIControlEventTouchUpInside];
- (IBAction)performModalActionForButton:(id)sender
{
NSString *string = /* get the string you want to send */;
[obj doSomethingWithString:string];
}