与IBAction链接的UIButton在Xcode 9.2中的物理设备和模拟器上无法模拟

时间:2018-01-10 02:14:55

标签: ios xcode uibutton swift4

我添加了带有Control + Drag的UIButton,为随机数添加了变量和arc4random,然后使用前置函数测试我的按钮以确保它有效。但是按钮完全没有响应,我的代码中没有错误。

import UIKit

class ViewController: UIViewController {

    var randomDiceIndex1 : Int = 0
    var randomDiceIndex2 : Int = 0

    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func rollButtonPressed(_ sender: UIButton) {

        randomDiceIndex1 = Int(arc4random_uniform(6))
        randomDiceIndex2 = Int(arc4random_uniform(6))

        print(randomDiceIndex1) //This line of code is what I used to test
    }
}

1 个答案:

答案 0 :(得分:0)

以编程方式分配操作。

yrBtnOutlet.addTarget(self, action: #selector(self. rollButtonPressed(_:)), for: .touchUpInside)

希望这会有所帮助。