我正在尝试创建一个函数,在应用程序启动时,应用程序检查是否有当前登录的Parse用户,如果这是真的,那么用户应绕过登录屏幕并转到我的视图位置在选项卡式视图控制器中。如果当前没有用户登录,则打开登录视图,默认情况下会在应用程序加载时打开。我通过在我的登录视图上的viewWillAppear函数中创建了一个if语句来破解这个功能,如果这是真的,那么instantiateViewControllerWithIdentifier()
,但由于我的名字没有标识符,我的应用程序立即崩溃了。
reason: 'Storyboard (<UIStoryboard: 0x7ffbf3e82190>) doesn't contain a view controller with identifier 'ProfileSettingsViewController'
现在我可能会遗漏一些东西,但是我在哪里设置视图控制器的标识符?或者我的班级名称是我目前假设的标识符?
在一个相关问题中,这是实现我的目标的最佳途径吗?
这是我的登录控制器逻辑:
override func viewWillAppear(animated: Bool) {
var currentUser = PFUser.currentUser()
if currentUser != nil {
self.storyboard?.instantiateViewControllerWithIdentifier("ProfileSettingsViewController")
}
}
如果用户已登录ProfileSettingsViewController.swift
:
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.
}
*/
}
答案 0 :(得分:7)
我之前没有使用过Parse,但是为了授权我通常会让AppDelegate处理初始视图控制器。
// 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;
}
我很确定这可以在视图控制器内部工作,因为在您的代码中,您没有将实例化的控制器分配给根视图控制器。但是,我认为AppDelegate应该注意更改Root View Controller,而不是UIViewControllers。
编辑:对不起,忘了提及你的错误。转到故事板并单击ProfileSettingsViewController并转到侧栏中的第3个选项卡,其中有一个Storyboard ID。将名称设置为&#34; ProfileSettingsViewController&#34; (或任何你想要的)它会找到控制器。UIStoryboard.instantiateViewControllerWithIdentifier
在故事板中搜索具有给定故事板ID的控制器。
答案 1 :(得分:0)
您可以在视图控制器上设置故事板标识符,位于故事板中的自定义类设置下。
我会加载主屏幕并检查用户是否在dispatch_once块中的viewDidAppear:
上登录,如果user == nil,则使用segue或手动显示loginViewController
。
当用户登录时执行倒带segue,通过代理发送消息或将通知发送回主屏幕以关闭并刷新。