消息传递的Objective C语法

Swa*_*nil 1 iphone objective-c

我正在尝试以下任务:

我知道我们可以通过使用按钮点击calla方法:

[button addTarget:self action:@selector(performModalActionForButton)
forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

方法如下:

(void) performModalActionForButton:(NSString *)str
{

}
Run Code Online (Sandbox Code Playgroud)

我想在按钮单击上发送一个字符串到一个方法.我怎样才能做到这一点?

谢谢

Bas*_*ian 5

您的代码中有两个小错误..

  1. 如果您使用@selector并想要选择带参数的函数,您可以添加:...所以它是@selector(performModalActionForButton:)

  2. 传递给函数的对象总是消息的发送者..所以函数将是:

    - (void) performModalActionForButton:(UIButton *)button
    {
    
    
    } 
    
    Run Code Online (Sandbox Code Playgroud)

在该功能中,您可以检查按钮上的文本然后...或标签或任何您想要的内容.

  • 关于(1),请注意Objective-C将冒号视为方法名称的一部分,因此`@selector(foo)`和`@selector(foo :)命名两种不同的方法. (3认同)