简体   繁体   English

如何在Swift 4中获取按钮值?

[英]How do i get the button value in swift 4?

    @IBOutlet var button: UIButton!    
    func randomize(){
        var x_axis:CGFloat = 8.0
        var y_axis:CGFloat = 330.0
        for selected_Synonym in selected_Synonyms {
            button = UIButton.init(type: UIButtonType.custom) as UIButton
            button.frame = CGRect(x: x_axis, y: y_axis, width: 400, height: 50)
            button.backgroundColor = UIColor.black
            button.setTitle(selected_Synonym as? String, for: UIControlState.normal)
            button.setTitleColor(UIColor.white, for: [])
            button.addTarget(self, action: Selector(("pressed:")), for: UIControlEvents.touchUpInside)
            self.view.addSubview(button)
            x_axis = 10.0
            y_axis += 70.0
        }
    }

    func pressed(sender: Any){
        let buttonTitle = button.currentTitle
        print(buttonTitle)
    }

However when it runs and I press on a button I get the following error: 但是,当它运行并按一个按钮时,出现以下错误:

Thread 1: signal SIGABRT. 线程1:信号SIGABRT。

The program creates 5 buttons. 该程序将创建5个按钮。 I am new to swift and ios development would be very grateful if someone could help me out. 我是Swift的新手,如果有人可以帮助我,iOS开发将不胜感激。 Thank you. 谢谢。

You have several issues. 你有几个问题。 To fix the crash, replace Selector(("pressed:")) with #selector(pressed) . 要解决崩溃问题,请用#selector(pressed)替换Selector(("pressed:")) #selector(pressed) The use of Selector is very out-of-date. Selector的使用已过时。 Always use #selector . 始终使用#selector

Next, remove the @IBOutlet var button: UIButton! 接下来,删除@IBOutlet var button: UIButton! line. 线。 You don't need it. 不用了

Then change: 然后更改:

button = UIButton.init(type: UIButtonType.custom) as UIButton

to: 至:

let button = UIButton(type: .custom)

Then update your pressed function to: 然后将您pressed功能更新为:

@objc func pressed(sender: UIButton){
    let buttonTitle = sender.currentTitle
    print(buttonTitle)
}

Note the addition of @objc . 请注意@objc的添加。 This is required for any function being used with a target/selector. 这是目标/选择器使用的任何功能所必需的。 Also note that sender is now UIButton instead of Any . 还要注意, sender现在是UIButton而不是Any It's best to set the sender's type to match the proper type. 最好将发送者的类型设置为匹配正确的类型。

Here's all of your code with lots of little fixes: 这是您的所有代码,其中包含许多小错误:

func randomize() {
    var xAxis: CGFloat = 8.0
    var yAxis: CGFloat = 330.0

    for selectedSynonym in selectedSynonyms {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: xAxis, y: yAxis, width: 400, height: 50)
        button.backgroundColor = .black
        button.setTitle(selectedSynonym, for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.addTarget(self, action: #selector(pressed), for: .touchUpInside)
        self.view.addSubview(button)
        xAxis = 10.0
        yAxis += 70.0
    }
}

@objc func pressed(sender: UIButton){
    let buttonTitle = sender.currentTitle
    print(buttonTitle)
}

Use camelCase, not snake_case when naming variables and functions. 命名变量和函数时,请使用camelCase,而不要使用snake_case。 Make use of Swift type inference. 利用Swift类型推断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM