且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IOS:一是IBAction为多个按钮

更新时间:2023-11-28 19:21:52

如果你使用界面生成器创建的按钮,只需在相关类相同的IBAction为指向他们。

If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class.

然后,您可以通过读取按钮上的文本的IBAction为方法中的按钮区分...

You can then differentiate between the buttons within the IBAction method either by reading the text from the button...

- (IBAction)buttonClicked:(id)sender {
    NSLog(@"Button pressed: %@", [sender currentTitle]);    
}

...或者通过设置标记属性X code和通过 [发送方标记] $ C回读$ C>。 (如果你使用这种方法,从1开始的标签,为0是默认的,所以很少使用。)

...or by setting the tag property in Xcode and reading it back via [sender tag]. (If you use this approach, start the tags at 1, as 0 is the default and hence of little use.)