如何从swift 3.0中的另一个按钮操作调用按钮操作

问题描述 投票:-2回答:4

我在button action以下

@IBAction func actionB(_ sender: UIButton) {
 print("something")
}

我想在按钮动作下面调用上面的按钮动作。

@IBAction func actionC(_ sender: UIButton) {
 //call to above button action in here
}

我怎样才能做到这一点。希望你对此有所帮助

ios uibutton swift3 ibaction
4个回答
0
投票

试试这个 :

 @IBAction func actionC(sender: AnyObject) {
        //call to above button action in here
        actionB(sender: sender);
    }

0
投票

尝试此解决方案

actionB("")

如果你想要你也可以这样做

actionB(sender)

要么

actionB(ButtonB)


0
投票

只需将您的代码更改为:

@IBAction func actionB(sender: AnyObject?) {
 print("something")
 actionC(nil)
}

@IBAction func actionC(sender: AnyObject?) {
 //call to above button action in here
}

注意AnyObject之后的可选标记。

要么

@IBAction func actionB(sender: AnyObject) {
 print("something")
}

@IBAction func actionC(sender: AnyObject) {
 //call to above button action in here
 actionB("" as AnyObject)
}

0
投票

这是做到这一点的方法......

@objc fileprivate func methodA(_ sender: UIButton) {
    print("method A")
}

@objc fileprivate func methodB(_ sender: UIButton) {
    methodA(sender)
}
© www.soinside.com 2019 - 2024. All rights reserved.