我正在尝试创建一个功能,在应用启动时,应用会检查当前是否有 Parse 用户登录,如果是,则用户应绕过登录屏幕并转到我位于的 View 在选项卡式 View Controller 中。如果当前没有用户登录,则打开默认在应用加载时打开的登录 View 。我通过在我的登录 View 的 viewWillAppear 函数中创建一个 if 语句来破解这个功能,如果这是真的,那么 instantiateViewControllerWithIdentifier()
,但是我的应用程序立即崩溃,因为我的名字没有标识符.
reason: 'Storyboard (<UIStoryboard: 0x7ffbf3e82190>) doesn't contain a view controller with identifier 'ProfileSettingsViewController'
现在我可能遗漏了一些东西,但是我应该在哪里设置 View Controller 的标识符呢?或者我的类名是我目前假设的标识符吗?
在相关问题中,这是实现我想要做的事情的最佳方式吗?
这是我的登录 Controller 逻辑:
override func viewWillAppear(animated: Bool) {
var currentUser = PFUser.currentUser()
if currentUser != nil {
self.storyboard?.instantiateViewControllerWithIdentifier("ProfileSettingsViewController")
}
}
如果用户已经登录ProfileSettingsViewController.swift
,下面是应该打开的 View :
import UIKit
class ProfileSettingsViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
self.tabBarController?.navigationItem.setHidesBackButton(true, animated:true);
//self.tabBarController?.navigationItem.title = "Profile Settings"
self.navigationController?.navigationItem.title = "ffff"
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func logoutButton(sender: AnyObject) {
PFUser.logOut()
var currentUser = PFUser.currentUser()
self.performSegueWithIdentifier("userLoggedOut", sender: self)
self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
最佳答案
我以前没有使用过 Parse,但为了授权,我通常让 AppDelegate 负责初始 View Controller 。
// member variables
var storyboard : UIStoryboard?;
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle());
var currentUser = PFUser.currentUser()
if currentUser != nil {
self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ProfileSettingsViewController");
}
return true;
}
我很确定这会在 View Controller 内部起作用,因为在您的代码中,您没有将实例化的 Controller 分配给 Root View Controller 。但是,我认为 AppDelegate 应该负责更改 Root View Controller ,而不是 UIViewControllers。
编辑:抱歉,忘记提及您的错误。转到 Storyboard并单击 ProfileSettingsViewController 并转到侧边栏中的第三个选项卡,那里有一个 Storyboard ID。将名称设置为“ProfileSettingsViewController”(或任何您想要的名称),它将找到 Controller 。 UIStoryboard.instantiateViewControllerWithIdentifier
在 Storyboard中搜索具有给定 Storyboard ID 的 Controller 。
关于ios - 如果解析用户已登录,则绕过登录 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856651/