iOS 将方法附加到按钮
例子
要将方法添加到按钮,请首先创建一个操作方法:
Objective-C
-(void) someButtonAction{ NSLog(@"Button is tapped"); }
Swift
func someButtonAction() { print("Button is tapped") }
现在要将此操作方法添加到您的按钮,您必须编写以下代码行:
ObjectiveC
[yourButtonInstance addTarget:self action:@selector(someButtonAction) forControlEvents:UIControlEventTouchUpInside];
Swift
yourButtonInstance.addTarget(self, action: #selector(someButtonAction), forControlEvents: .touchUpInside)
对于ControlEvent,ENUMUIControlEvent的所有成员均有效。