如果已分析用户登录,则绕过登录视图 [英] Bypass Login View if Parse User is Logged In

查看:62
本文介绍了如果已分析用户登录,则绕过登录视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,以便在启动应用程序时,该应用程序检查是否当前有Parse用户登录,如果为true,则该用户应绕过登录屏幕并转到我位于的视图在选项卡式视图控制器中.如果当前没有用户登录,则打开登录视图,默认情况下,该视图在应用程序加载时打开.我通过在登录视图的viewWillAppear函数内创建一个if语句来破解此功能,如果为true,则为instantiateViewControllerWithIdentifier(),但是由于我的名字没有标识符,我的应用程序立即崩溃了.

I am trying to create a function where upon app launch, the app checks to see if there is a Parse user currently logged in, and if this is true, then the user should bypass the login screen and taken to my view located within a tabbed view controller. If there is not user currently logged in, then open the login view, which opens on app load by default. I took a crack at this functionality by creating an if statement within the viewWillAppear function on my login view and if this is true, then instantiateViewControllerWithIdentifier(), but my app crashes immediately due to no identifier with my name.

reason: 'Storyboard (<UIStoryboard: 0x7ffbf3e82190>) doesn't contain a view controller with identifier 'ProfileSettingsViewController'

现在我可能会丢失一些东西,但是我应该在哪里设置视图控制器的标识符?还是我目前假设的班级名称是标识符?

Now I might be missing something, but where do I set the identifier for a view controller? Or is my Class name the identifier as I currently assume?

在一个相关的问题中,这是实现我想要做的最好的方法吗?

In a related question, is this the best way to achieve what I am looking to do?

这是我的登录控制器逻辑:

Here is my login controller logic:

override func viewWillAppear(animated: Bool) {

        var currentUser = PFUser.currentUser()

        if currentUser != nil {
           self.storyboard?.instantiateViewControllerWithIdentifier("ProfileSettingsViewController")   
        }        
}

如果用户已经登录ProfileSettingsViewController.swift,则应在此处打开该视图:

Here is the view that should open if user is already logged in 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.
    }
    */

}

推荐答案

我以前没有使用过Parse,但是为了获得授权,我通常让AppDelegate负责初始视图控制器.

I haven't used Parse before, but for authorization I generally let AppDelegate to take care of the initial 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;
}

我非常确定这将在视图控制器内部起作用,因为在您的代码中,您没有将实例化的控制器分配给根视图控制器.但是,我认为AppDelegate应该注意更改Root View Controller,而不是UIViewControllers.

I am pretty sure this will work from inside the view controller because in your code, you didn't assign the instantiated controller to the root view controller. However, I think AppDelegate should take care of changing the Root View Controller, not UIViewControllers.

对不起,忘记提及您的错误.转到情节提要,然后单击ProfileSettingsViewController并转到侧栏中的第3个选项卡,然后会有一个情节提要ID.将名称设置为"ProfileSettingsViewController"(或任何您想要的名称),它将找到控制器. UIStoryboard.instantiateViewControllerWithIdentifier在情节提要中搜索具有给定情节提要ID的控制器.

Sorry, forgot to mention your error. Go to the storyboard and click on the ProfileSettingsViewController and go to the 3rd tab in the sidebar and there is a Storyboard ID. Set the name to "ProfileSettingsViewController" (or whatever you want) and it will find the controller. UIStoryboard.instantiateViewControllerWithIdentifier searches the storyboard for a controller with the given Storyboard ID.

这篇关于如果已分析用户登录,则绕过登录视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆