Launch Watch App into middle view

瘦欲@ 提交于 2019-12-22 04:45:10

问题


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.


回答1:


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

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.




回答2:


Update Swift 3.0

class CenterPageViewController: WKInterfaceController {

override init (){
    super.init()
    super.becomeCurrentPage()
  }
}

This will works...!!!

Thanks




回答3:


The new way to do this in watchOS 4 and higher is:

WKInterfaceController.reloadRootPageControllers(withNames: 
["Controller1" "Controller2", "Controller3"], 
 contexts: [context1, context2, context3],
 orientation: WKPageOrientation.horizontal,
 pageIndex: 1)

Now you don't get the annoying animation when using becomeCurrentPage() when you want to start with the middle page.



来源:https://stackoverflow.com/questions/27097022/launch-watch-app-into-middle-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!