更改Xcode 9 swift 4中的按钮标题时出现sigabrt错误

时间:2018-09-17 16:39:06

标签: ios swift xcode

我有一个带有UIImage和UIButton的屏幕。我编写了代码,以便单击按钮时图像会更改并且可以正常工作。然后,我想添加代码来制作按钮的标题。在我添加了该代码之后,该程序因sigabrt错误而崩溃。我检查了连接,一切看起来都很好。我如何更改按钮的文本/标题而不会崩溃。

class MapViewController: UIViewController {

@IBOutlet weak var mapButton: UIButton!
@IBOutlet weak var mapImage: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    //mapButton.setTitle("test", for: .normal)
    // Do any additional setup after loading the view.
}
@IBAction func mapButtonTapped(_ sender: UIButton) {

    if x == true
    {
        mapImage.image = #imageLiteral(resourceName: "map")
        x = false
        mapButton.setTitle("1st Floor Map", for: .normal)
    }
    else
    {
        mapImage.image = #imageLiteral(resourceName: "School Map 2015-2016-2")
        mapButton.setTitle("2nd Floor Map", for: .normal)
        x = true
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
 var x:Bool = true;

Connection tab

1 个答案:

答案 0 :(得分:0)

mapButtonTapped(_ sender:)中,您可以更改

mapButton.setTitle("2nd Floor Map", for: .normal)

sender.setTitle("2nd Floor Map", for: .normal)

sender是您点击的按钮,并且您已将类型选择为UIButton,您可以将其属性作为UIBUtton进行访问。

关于sigabrt错误,您应该尝试here