我想知道在app编程中需要更改根视图控制器的位置。以下是我的代码
class NavigationViewController: UINavigationController {
var window: UIWindow?
override func viewDidLoad() {
super.viewDidLoad()
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let newRootView = storyBoard.instantiateViewController(withIdentifier: "WelcomeViewController") as! WelcomeViewController
let userStatus = UserDefaults.standard.bool(forKey: "isLoggedIn")
if userStatus {
self.window?.rootViewController = newRootView
}
}
}
我在用户登录时将isLoggedIn键的值设置为true,在用户注销时将其设置为false,以便我可以根据我可以选择根视图控制器来检索此导航控制器子类中的值。但它不能正常工作,每当我启动应用程序时都会保持不变我登录页面虽然我已经登录,但也请不要建议我更改didfinishlaunchingwithoptions方法中的根视图控制器。有人可以帮我解决吗?提前谢谢。
答案 0 :(得分:1)
如果我正确理解了代码的目标,那么如果用户已经登录,则需要直接在lathe.cuttingTime
启动。
您可以尝试创建名为WelcomeViewController
的自定义segue,并将此ReplaceSegue
设置为链接ReplaceSegue
和NavigationViewController
的segue。
然后你可以改变segue的目的地。
ViewController
答案 1 :(得分:-1)
首先在NSUserDefults中设置用户登录bool true。用户登录成功时。
UserDefaults.standard.set(true, forKey: "USERISLOGIN")
现在打开Appdelegate.swift - > didFinishLaunchingWithOptions方法: -
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if(UserDefaults.standard.bool(forKey: "USERISLOGIN")){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let objLogoutVC = storyboard.instantiateViewController(withIdentifier: "LogoutVC") as! LogoutVC
let navigationController = UINavigationController(rootViewController: objLogoutVC)
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.isHidden = true
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
return true
}
如果您需要更多的东西,请告诉我。