How can I disable the user interaction for the Cancel button of my watchOS interface controller?

3.3k views Asked by At

In my Apple Watch app, one of my interface controllers has a Cancel button at the top left corner. In my case, once a particular action is completed, I don't want the user to go back to the previous screen, so I want to disable the user interaction for that Cancel button. Even if I change the title to an empty string, user interaction still stays enabled.

3

There are 3 answers

4
pteofil On

You can't disable user interaction for back button.

But you can change a bit the way you present your views to accomplish what you want.

Start with your normal view. Check if you need to show the user the login. If you do, then present the login view Modally. At the end of login you close the modal view and you're back to normal view, without unnecessary back button.

0
Tamás Sengel On

This is simbesi.com's answer in watchOS 7/Swift 5.

Presenting the new controller modally:

presentController(withName: "NewInterfaceController", context: nil)

Presenting the new controller by replacing the root controller:

WKInterfaceController.reloadRootControllers(
    withNamesAndContexts: [
        (
            name: "NewInterfaceController",
            context: "NewInterfaceController" as AnyObject
        )
    ]
)
1
simbesi.com On

We can't disable the back/cancel button user intaraction but can load controller without cancel button.

presentControllerWithName("NewInterfaceController", context: nil)

presentControllerWithName this will present controller with cancel button. If we use like below won't get the cancel button.

WKInterfaceController.reloadRootControllersWithNames(["NewInterfaceController"], contexts: ["NewInterfaceController"])

reloadRootControllersWithNames this will make our controller as a root controller so we won't get cancel button. This is how i resolved my issue. Hope it will help you as well.

NOTE: here [ ] is the syntax. exp: ["NewInterfaceController"]