2016-09-16 44 views
0

我有四个按钮,我希望用户按。按钮值是1,5,25和100.如何将故事板中的多个按钮连接到Swift 3中的单个动作以增加值?

我有一个标签设置为显示按下按钮的所有值的总和。

但是,由于IBAction按钮的代码中有公式,因此数字不会相加。我在之前的帖子中看到,过去常常会出现一个“连接动作”,这将能够将故事板中的多个IBAction按钮连接到Swift的标志动作。我无法在Swift 3中找到该功能。

那么,如何将故事板中的多个IBActions按钮连接到Swift 3中的单个操作?后

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

var oneButtonTapped = 0 
var noButtonTapped = 0 
@IBAction func oneButtonTapped(_ sender: AnyObject) {oneButtonTapped = oneButtonTapped + 1 

//Aware that the location of the code is requiring the 1 button to be tapped to be update. TODO: Have the total update after every button tap 

    let totalAmount = (noButtonTapped + oneButtonTapped + fiveButtonTapped + twentyfiveButtonTapped + hundredButtonTapped) 

    Amount.text = String(totalAmount) 
} 
var fiveButtonTapped = 0 
@IBAction func fiveButtonTapped(_ sender: AnyObject) {fiveButtonTapped = fiveButtonTapped + 5 
} 
var twentyfiveButtonTapped = 0 
@IBAction func twentyfiveButtonTapped(_ sender: AnyObject) {twentyfiveButtonTapped = twentyfiveButtonTapped + 25 
} 
var hundredButtonTapped = 0 
@IBAction func hundredButtonTapped(_ sender: AnyObject) {hundredButtonTapped = hundredButtonTapped + 100 
} 

预先感谢您的协助

下面的代码放置,这是非常感谢!

回答

1

这看起来很相似这个线程Connecting Multiple Buttons to a single IBAction。基本上,您可以使用Autolayout的连接工具来将故事板上的多个按钮“控制拖动”到先前创建的swift代码中的IBAction。

此链接Can multiple buttons be connected to a single IBAction有一些动画GIF显示连接过程。

然后,您可以使用“sender”参数来引用触发代码的按钮。

就像旁边它看起来好像你的代码坐在didReceiveMemoryWarning()函数内。

希望这会有所帮助。

相关问题