AziCode
AziCode

Reputation: 2682

How to send data back to the previous interface controller?

I would like to update the button title (grey button) in the first interface controller by pressing on the blue button in the second interface controller.

Should I do something like that:

var counter = 1

@IBAction func addOne() {

   greyButtonLabel.setTitle("\(counter++)")
   pushControllerWithName("secondController", context : add)
}


// The second interface controller

override func awakeWithContext(context: AnyObject?) {
      super.awakeWithContext(context)    

      if let addone = context as? counter {
         greyButtonLabel.setTitle("\(counter++)")
      }
}

enter image description here

Upvotes: 3

Views: 711

Answers (1)

John
John

Reputation: 8538

You cannot change a UI element in the first interface controller when it is not active.

Here is one possible way:

  • Send self of the first interface controller to the second one (using pushControllerWithName("secondController", context: ... )).
  • Update a property in the first interface controller while the second interface controller is active.
  • You may programmatically go back to the first interface controller by calling the second interface controller's popControllermethod.
  • When the first interface controller is activated, read out the property and update the button accordingly (in the method willActivate).

Upvotes: 1

Related Questions