簡體   English   中英

DrawerController的iOS狀態恢復問題

[英]iOS state restoration issue with DrawerController

我有一個使用Xcode 8.3.3在Swift 3.1中編寫的應用程序。

我目前正在嘗試實施國家保護/恢復。

為此,我在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
}

我還為所有相關的viewcontrollers和navigationcontrollers提供了恢復ID。

我正在使用第三方庫來處理側抽屜導航容器( https://github.com/sascha/DrawerController )。 初始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
}

當應用程序打開並嘗試恢復狀態時,它會暫時顯示正確的viewcontroller(應用程序關閉前的最后一個控制器),然后一旦應用程序變為活動狀態,它將恢復為初始viewcontroller。

例如,發生以下情況:

  1. 打開應用程序通過側面菜單導航到“設置”視圖
  2. 導航到主屏幕
  3. 停止運行xcode並重新啟動它
  4. 應用程序將打開顯示設置視圖,然后恢復為主視圖

任何人都可以告訴我是什么導致了這個,或者我哪里出錯了? 如果您需要更多代碼示例,請與我們聯系。

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