Stefan
Stefan

Reputation: 5451

How can I combine "popToRootController" with "pushController"

I have 3 InterfaceControllers in my WatchApp:

The App starts in the StartInterfaceController and navigates to "Target1" InterfaceController, if the users touches the button.

By pressing the button in "Target1" InterfaceController I want to programatically navigate back to "Start" and then open "Target2"

enter image description here

I tried this code:

In "Target1":

@IBAction func navigateToTarget2() {
    navigateBack = true // navigateBack is a global variable
    popToRootController()
}

In "Start":

override func didAppear() {
    if navigateBack {
        navigateBack = false
        pushController(withName: "Target2", context: nil)
    }
}

The first part is working well. Pressing the button in "Target" navigates back to the "Start". The code in start is executed (I've checked in the debugger) but the navigation to "Target2" is not working. I've also checked the name of the target in the pushController operation. It has the correct name.

Upvotes: 0

Views: 194

Answers (1)

Parth Adroja
Parth Adroja

Reputation: 13514

Combination of push and pop won't be done at a time.

Alternatively, what you can do is when you press button on "Start" go to "Target1" by pushing on "Start" controller with nil context. When you press the button on "Target1" controller you can call reloadRootControllersWithNames and pass some context over there and make a condition that isFrom "Target1" button so navigate to "Target2".

Example: It's not an actual code.

class StartVC: WKInterfaceController {

 func buttonAction() {
    self.push("TargetOneVC", context: nil)
 }

}

class TargetOneVC: WKInterfaceController {

 func buttonAction() {
     WKInterfaceController.reloadRootControllersWithNames(["TargetTwoVC"], 
     contexts: [isFromTargetOne: true])
  }
}

class TargetTwoVC: WKInterfaceController {

}

Upvotes: 1

Related Questions