Search code examples
iosswiftuibuttoniboutlet

Simple button on second view controller crashing app/ Swift


I have a very simple app so far. Two view controllers. I've set up a new .swift file for the second view. On each view I have a button that when pressed, changes a label to say "Pressed". Pretty simple.

On the first view controller everything works as expected. However, on the second view controller the app crashes when I press the button. I've set up IBOutlets and actions for all appropriate parts.

Does anyone have any insight?

code:

import UIKit

class PlayViewController: UIViewController {


@IBOutlet weak var newCardButton: UIButton!
@IBOutlet weak var labelTest: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func newCardButtonPressed(sender: UIButton) {

    self.labelTest.text = "Pressed"
}

}

Screenshot:

enter image description here

Screenshot after crash- https://i.sstatic.net/V1ZCd.png


Solution

  • I think you should change the sender part like this.

        @IBAction func newCardButtonPressed(sender: AnyObject) {
    
        self.labelText.text = "Pressed"
    }
    

    If your connections are not set properly your app also crash. Delete them and reconnect it.From Utilities/connections inspector.