ios - 如果解析用户已登录,则绕过登录 View

标签 ios swift parse-platform

我正在尝试创建一个功能,在应用启动时,应用会检查当前是否有 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/

相关文章:

ios - iOS 中如何设置图像的长宽比

ios - 如何在 2 个 TableView 单元格之间创建空间?

ios - 为什么删除子项后我的表格 View 显示重复的单元格?

ios - 如何确保提交给 parse.com 的非登录用户数据仅来自移动应用

iphone - UITabBarController提供的模态viewController

iphone - 我的代码在使用核心数据时崩溃并出现 'index beyond bounds' 错误

ios - 如何在 swift 中导入 JSQMessagesViewController?

ios - 使用从 AppDelegate 传递的参数覆盖 ViewController viewDidLoad 方法参数

swift - 关闭解析推送状态

ios - 如果 PFQuery 已经在运行,则忽略获取