Why can't I send data between interface controllers for Apple Watch?

Basically what I am trying to do is pass an integer from one interface controller. The slider in the SecondPage controller will receive an integer value from the slider and should send it to an interface controller called ThirdPage and set the title of this shortcut to a value. I have tried so many methods but all to no avail. I'm just trying to send the context from the second controller of the interface, get it in wake mode (using the context context: anyone?) In the third controller, and keep it as a title until the slider is changed again.

Here's what I tried first in the second page controller, but it didn't pass any data:

override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
if segueIdentifier == "ThirdPage"{
    return sliderval
}
return nil
}

      

here is what i tried after that, it passed the data, but didn't "save" it. It outputs the interface controller with the updated label to the value of the slider, but I don't want it to pop up, I want that value to persist in the label. When I close the popup and scroll right down to the third page, the label is still called "Shortcut"

self.presentController(withName: "ThirdPage", context: sliderval)

      

Here are some output images as well as some of my code. Any help is appreciated, thanks. PS: This is all in Swift 3enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

+3


source to share


2 answers


You are using page based navigation and you need to pass data between pages.

What I would do is save the data from the slider somewhere that the third page can refer to, and populate the data in the label on willActivate

. The method willActivate

should be called by the system when the user is browsing between pages.

In the global scope, create a variable to hold the value of the slider.

var sliderValue: Float = 0

      

In your second front-end controller, set this value when you change the slider.



@IBAction func sliderChanged(_ value: Float) {
    sliderValue = value
}

      

In your third frontend controller, when the page is about to appear, set the label to the correct value.

override func willActivate() {
    label.setText("\(sliderValue)")
}

      

If you want to avoid using globals, you can read and write from custom defaults or use some other tactic, but this will depend on the exact needs of your application.

+1


source


The unwanted popup issue happens because you are calling presentController()

in sliderChanged()

. Each time the slider changes, you tell it to bring up the Third Page controller. You can simply delete it.

Edit: My answer incorrectly suggested that asser works with hierarchical navigation. For page navigation, there seems to be no way to pass data from one page to another, so you are taking a global approach as the accepted answer suggests. The original answer follows:



As for the lack of transmission context, I would assume that you probably don't have the segue id set in Interface Builder, or set to something other than "ThirdPage". The segment is separate from the "ThirdPage" controller, and you must set its ID separately. Click on the circle in the middle of the segue arrow and take a look at it in the attribute inspector.

Assuming I am correct, the problem is what is being contextForSegueWithIdentifier()

called with an ID that you are not expecting. Since it is not "ThirdPage", the condition fails and returns nil. Setting the segue id should fix it.

+1


source







All Articles