我在for循环中动态创建了五个按钮。现在我想将一个OnClick事件与执行不同操作的每个按钮关联起来。我如何做到这一点,以及如何检测哪个按钮被点击?iPhone中的多个按钮onclick事件中的问题
for (NSUInteger i=0;i<5;i++)
{
UIButton *myButton1 = [[UIButton buttonWithType:UIButtonTypeCustom]
initWithFrame:CGRectMake(5, 57,15, 15)];
[myButton1 setTitle:@"Click Me!" forState:UIControlStateNormal];
[myButton1 addTarget:self action:@selector(buttonClicked1:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton1];
}
FWIW:“构建32位应用程序时,NSInteger是一个32位整数,64位应用程序将NSInteger视为一个64位整数。” http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/uid/20000018-DontLinkElementID_8 – 2009-10-23 09:26:57
阿哈,有甚至一个关于他们的SO线程:http://stackoverflow.com/questions/13725。 – zoul 2009-10-23 09:57:16