3

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.

  • I was able to use the counter in order to update the label, but how would I do to send the result back to the first interface Controller

  • should it be executed in the second interface controller and then the result is sent back through the push segue

  • How would I use pushControllerWithName("secondController",context: ... )

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

John
  • 8,468
  • 5
  • 36
  • 61
AziCode
  • 2,510
  • 6
  • 27
  • 53
  • Possible duplicate of [Passing data back from a modal view in WatchKit](https://stackoverflow.com/questions/27021186/passing-data-back-from-a-modal-view-in-watchkit) – BootMaker Sep 26 '17 at 13:12
  • @BootMaker the code in the other question is in objectiveC – AziCode Sep 26 '17 at 22:49

1 Answers1

1

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).
John
  • 8,468
  • 5
  • 36
  • 61
  • 2
    To add to vomako's answer, this might help you understand the life cycle of what it means for a `WKInterfaceController` to be active: http://blog.mikeswanson.com/post/118262770484/watchkit-controller-life-cycle – Mike Swanson May 11 '15 at 21:59
  • 1
    Thanks, Mike, great addition like always! – John May 11 '15 at 22:00
  • Thank you guys, I will try to make it work and let you know what happened. – AziCode May 11 '15 at 22:02
  • @MikeSwanson : Should I create a new Push segue in the second controller and put it inside the IBAction ( So that when I click on Add1, it jumps back to the first Interface Controller and updates the label? ) – AziCode May 11 '15 at 22:22
  • 1
    You should dismiss the second interface controller by calling its `popController` method. Don't initiate a new push segue. – John May 11 '15 at 22:31
  • 1
    In addition to Mike's great blog, you may also refer to the documentation: https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceController_class/#//apple_ref/occ/instm/WKInterfaceController/popController – John May 11 '15 at 22:33
  • @vomko : I'm sending self as the context to the second interface controller , and then i only have a popController () in the IBAction ( of the 2nd controller) . after that i've put : buttonLabel.setTitle("\(count++)") in the willActivate and I got the desired result. Is it a good way to do it? – AziCode May 11 '15 at 23:07
  • 1
    When you do it like this, the first IC will show a higher count when you press the button. So far so good. However, it will also show a higher count when the user taps on the back icon. Therefore, I would increase the value of a property in the first interface controller in the IBAction method of the second interface controller and then use the value of this property to update your label when the first view controller is activated. – John May 11 '15 at 23:12