Search code examples
objective-cwatchkitapple-watch

Get WatchKit interface controller instances created with presentControllerWithNames:contexts:


I'm presenting a page based modal using [self presentControllerWithNames:self.controllerNames contexts:self.controllerContexts];, where controllerNames is just an NSArray of NSStrings containing my interface controllers names. The problem is that I would like to access the created controllers.

The documentations says that

WatchKit loads and initializes the new interface controllers and animates them into position on top of the current interface controller.

but I would like to have a reference to them, in order to call the becomeCurrentPage method from outside.

So, I would like to be able to save those controllers after they are created in a list, and programmatically change the page using something like [self.controllers[2] becomeCurrentPage].


Solution

  • Because you're allowed to provide a context when you present an interface controller, you can pass a reference to self. That way, you can establish a reference from the presented controller to its parent. Once that relationship exists, you can use things like delegation patterns to communicate.

    I use this extensively in my own Watch app, and I've wrapped a lot of these mechanics in my JBInterfaceController subclass on GitHub.