简体   繁体   English

DrawerController的iOS状态恢复问题

[英]iOS state restoration issue with DrawerController

I have an app written in Swift 3.1, using Xcode 8.3.3. 我有一个使用Xcode 8.3.3在Swift 3.1中编写的应用程序。

I am currently trying to implement state preservation/restoration. 我目前正在尝试实施国家保护/恢复。

To do this I have implemented shouldSaveApplicationState and willFinishLaunchingWithOptions methods in AppDelegate.swift and set to return true : 为此,我在AppDelegate.swift实现了shouldSaveApplicationStatewillFinishLaunchingWithOptions方法,并设置为返回true

// AppDelegate.swift
// STATE RESTORATION CALLBACKS

func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
    debug_print(this: "shouldSaveApplicationState")
    return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
    debug_print(this: "shouldRestoreApplicationState")
    restoringState = true
    return true
}

func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    debug_print(this: "willFinishLaunchingWithOptions")
    return true
}

I've also provided restoration IDs for all involved viewcontrollers and navigationcontrollers. 我还为所有相关的viewcontrollers和navigationcontrollers提供了恢复ID。

I'm using a 3rd party library to handle side drawer navigation container ( https://github.com/sascha/DrawerController ). 我正在使用第三方库来处理侧抽屉导航容器( https://github.com/sascha/DrawerController )。 The initial viewcontroller is set programmatically inside the didFinishLaunchingWithOptions method, see below. 初始viewcontroller是在didFinishLaunchingWithOptions方法中以编程方式设置的,见下文。

// AppDelegate.swift
var centerContainer: DrawerController?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let centerViewController =   mainStoryboard.instantiateViewController(withIdentifier: "RootViewControllerNav") as! UINavigationController 
    let leftViewController = mainStoryboard.instantiateViewController(withIdentifier: "SideDrawerViewController") as! UITableViewController

    centerContainer = DrawerController(centerViewController: centerViewController, leftDrawerViewController: leftViewController)

    centerContainer?.restorationIdentifier = "DrawerControllerView"
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.restorationIdentifier = "MainWindow"
    window?.rootViewController = centerContainer
    window?.makeKeyAndVisible()

    return true
}

When app opens and attempts to restore state, it displays the correct viewcontroller (last controller before app closed) temporarily, then once app becomes active it reverts back to the initial viewcontroller. 当应用程序打开并尝试恢复状态时,它会暂时显示正确的viewcontroller(应用程序关闭前的最后一个控制器),然后一旦应用程序变为活动状态,它将恢复为初始viewcontroller。

For example, the following happens: 例如,发生以下情况:

  1. Open app Navigate to the “settings” view via the side menu 打开应用程序通过侧面菜单导航到“设置”视图
  2. Navigate to the home screen 导航到主屏幕
  3. Stop running xcode and start it again 停止运行xcode并重新启动它
  4. App will open showing settings view, then revert back to home view 应用程序将打开显示设置视图,然后恢复为主视图

Can anyone tell me what is causing this, or where I am going wrong? 任何人都可以告诉我是什么导致了这个,或者我哪里出错了? Let me know if you need any more code examples. 如果您需要更多代码示例,请与我们联系。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let leftSideDrawerViewController = mainStoryboard.instantiateViewController(withIdentifier: "SideDrawerViewController") as! UITableViewController
        let centerViewController = mainStoryboard.instantiateViewController(withIdentifier: "RootViewControllerNav") as! UINavigationController
        let navigationController = UINavigationController(rootViewController: centerViewController)
        navigationController.restorationIdentifier = "navigationControllerKey"
        let leftSideNavController = UINavigationController(rootViewController: leftSideDrawerViewController)
        leftSideNavController.restorationIdentifier = "LeftNavigationController"
        self.drawerController = DrawerController(centerViewController: navigationController, leftDrawerViewController: leftSideNavController, rightDrawerViewController: nil)
        self.drawerController.openDrawerGestureModeMask = .all
        self.drawerController.closeDrawerGestureModeMask = .all
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = self.drawerController
        return true
    }     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM