如何在登录后重新打开应用程序
我是一名android开发人员。在android中,当用户在应用程序中登录时,我将重新打开MainActivity
类(控制器)以刷新一些视图。如何在登录后重新打开应用程序
在iOS应用程序中,如何做这个场景?
您可以重新打开您的预设/ LandingViewController
。 假设你有一个视图控制器与名称LandingViewController
当您成功登录的所有你需要的是重新实例化LandingViewController
在AppDelegate类进行函数名
func userDidLoggedIn(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)//Replace Main With your own storyboard name containing LandingViewController
let landingViewController = storyboard.instantiateViewController(withIdentifier: "LandingViewControllerIdentifier")//Pass your LandingViewController Identier that you have set in your storyboard file.
guard let subviews = self.window?.subviews else {
return
}
for view in subviews {
view.removeFromSuperview()
}
self.window?.rootViewController = landingViewController
}
现在只需在何地调用这个函数像这样的整个项目在你的情况下,在登录请求API的完成块中写下面的行。
let delegate = UIApplication.shared.delegate as! AppDelegate
delegate. userDidLoggedIn()
记录是什么''self.window后? –
窗口对象为应用程序的用户界面提供背景,并提供重要的事件处理行为。 Windows没有自己的视觉外观,但它们对于呈现应用视图至关重要。它拥有您的视图,使其可以在屏幕上显示。由于AppDelegate是一个单例类,所以会有一个窗口对象,它是您的默认应用程序窗口。 –
我该如何初始化它?因为我得到这样的消息:'类型'LoginController'的值没有成员'window'' –
一旦用户登录,你可以改变你的RootViewController的这样的:
var nav_VC: UIViewController?
func onSuccessfulLogin()
{
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
nav_VC = nil
if nav_VC == nil {
nav_VC = storyboard.instantiateViewController(withIdentifier: "home_nav")
}
self.window?.rootViewController = nav_VC
self.window?.makeKeyAndVisible()
}
ü需要设置视图RootViewController的在 – Koushik