如果您在Interface Builder(storyboard或xib)中设计视图控制器,我们可以将我们的按钮挂钩到我们想要的任意数量的操作.
使用ctrl + drag方法,我们创建了一个处理按钮按下的方法:
我们可以根据需要将尽可能多的方法连接起来:
这里,这个按钮连接到三种不同的方法.
我们可以通过编程方式在代码中执行相同的操作.
在Swift中:
myButton.addTarget(self, action: "methodOne:", forControlEvents:.TouchUpInside)
myButton.addTarget(self, action: "methodTwo:", forControlEvents:.TouchUpInside)
myButton.addTarget(self, action: "methodThree:", forControlEvents:.TouchUpInside)
Run Code Online (Sandbox Code Playgroud)
或者在Objective-C中:
[myButton addTarget:self
action:@selector(methodOne:)
forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self
action:@selector(methodTwo:)
forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self
action:@selector(methodThree:)
forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
我们可以连接任意数量的事件.
作为最后一点,我不确定是否有任何方法可以实际控制直接连接到按钮的方法被调用的顺序.在这种情况下,我迷上了按钮的方法依次是:one
,two
,three
,但他们出现在不同的顺序被称为:one
,three
,two
.它们始终按此顺序调用.
如果订单实际上很重要,那么我们应该将我们的按钮挂钩到一个方法,然后以我们需要的显式顺序调用所有其他方法:
@IBAction func buttonPress(sender: AnyObject) {
self.methodOne(sender)
self.methodTwo(sender)
self.methodThree(sender)
}
Run Code Online (Sandbox Code Playgroud)
说实话,我会说这应该是推荐的方法.它可能使源代码更容易阅读.
归档时间: |
|
查看次数: |
70 次 |
最近记录: |