Page Based “reloadRootControllersWithNames:” on launch loop?

前端 未结 4 1178
广开言路
广开言路 2020-12-10 05:05
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
    [WKInterfaceController reloadRootControllersWithNames:@[@\"pageOne\", @\"pageTwo\"] cont         


        
4条回答
  •  执念已碎
    2020-12-10 05:57

    calling WKInterfaceController.reloadRootControllers causes the awake function to be called a second time. This is the solution I use - it is straight forward, compact, and eliminates the recursive loop. This example has two page based views called mainControls and nowPlaying that are configured with contexts. Note the key thing here is to configure the mainControls view controller with an empty string context then that context is checked and returns if it is being called again due to the WKInterfaceController.reloadRootControllers statement that configured the context to "". Note the first time awake runs the context for the main view controller will be nil. Also note the second context is an implementation detail specific to my implementation - this could be any object that you want to pass to the second view controller.

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        if let _ = context as? String {
            print("already configured!")
            return
        }
        print("configuring...")
        WKInterfaceController.reloadRootControllers(withNames: ["mainControls", "nowPlaying"], contexts: ["", interaction])
    }
    

提交回复
热议问题