-1

I have a screen with a UIImage and a UIButton on it. I wrote code so that when the button is clicked the image changes and it worked. Then I wanted to add code to make the title of the button as well. After I added that code the program crashes with a sigabrt error. I checked the connections and everything look fine. How would I change the text/title of a button without having it crash.

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

Benjamin
  • 13
  • 3
  • Please add the error log – Adarsh V C Sep 17 '18 at 16:42
  • Use debug mode, set breakpoint – E.Coms Sep 17 '18 at 16:45
  • This might be related to your use of image literals, and the possibility that they’re not in the main bundle. Try replacing these with `mapImage.image = UIImage(named: “1st Floor Map”)`. Other possibility is a duplicate (broken) connection in your storyboard. – JonJ Sep 17 '18 at 16:52

1 Answers1

0

In the mapButtonTapped(_ sender:), you can change

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

to

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

sender here is the button which you tapped, and you had chosen the type to UIButton, you can access its properties as UIBUtton.

About the sigabrt error, you should try here.

Đinh Quân
  • 71
  • 2
  • 8
  • This solved the problem. Why does sender work instead of mapButton though? Aren't they the same thing? – Benjamin Sep 17 '18 at 16:58
  • I think the problem is by your mapButton linked to storyboard (duplicate or the missing connection may be). In this case, they are the same. – Đinh Quân Sep 17 '18 at 17:04
  • Wouldn't there be an "!" next to the connection if it was a duplicate or an empty circle if it was missing? I didn't see either. I added a picture. – Benjamin Sep 17 '18 at 17:17
  • Open both of your storyboard and viewController in Assistant Editor mode, if the circle before `@IBOutlet weak var mapButton: UIButton!` is empty, means you missed the connection. – Đinh Quân Sep 17 '18 at 17:20
  • I don't see any empty circles – Benjamin Sep 17 '18 at 17:22