Search code examples
iosswiftwatchkitapple-watch

Launch Watch App into middle view


Basically, my app is laid out in the page format and I would like it to launch into the middle of the three pages. There is no way of setting a previous page segue, so I have been trying to do it in code.

I have the main view set to the first view, and I have tried a variety of methods to segue to the middle view as soon as the app is launched.

Here is the two ways I tried:

    if segueCheck == true {
        self.pushControllerWithName("budget", context: self)
        self.presentControllerWithName("budget", context: self)
        segueCheck = false
    }

The first presents the view, but as a completely separate view, and the second replaces the first view with the middle view.

Does anyone know how I can launch into the middle view and allow the user to swipe left and right of it?

Thanks.


Solution

  • WKInterfaceController's becomeCurrentPage() should be what you're looking for.

    Let's create a new class for the center view controller, CenterPageViewController, and change its initWithContext: method as follows

    import WatchKit
    
    class CenterPageViewController: WKInterfaceController {
    
        override init(context: AnyObject?) {
            super.init(context: context)
    
            super.becomeCurrentPage()        
        }
    } 
    

    Now let's set the Custom Class for the middle page in your storyboard to CenterPageViewController

    enter image description here

    and finally hit run.

    You won't be able to get rid of the initial transition from the left page to the center page, but the app will finally begin on the middle page.