ios - 如何使用 PFUser 更改初始 View Controller ?

标签 ios swift parse-platform

我试图避免整体开始/登录屏幕,并在用户登录时直接进入应用程序的第一页。当我运行下面的代码时,没有任何反应。我断点了,它说当前用户为nil,但是当 View 加载时,当前用户不是nil。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    var currentUser = PFUser.currentUser()
    print("user \(currentUser)")
    if currentUser != nil {
        if(currentUser?.username?.rangeOfString("@music.co") != nil){
           self.storyboard?.instantiateViewControllerWithIdentifier("venuehomescreen")
        }else{
            self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("userhomescreen")
        }
    }
}

最佳答案

我通常在我的应用程序中正常处理登录 View Controller ,而不是在该登录 View Controller 的 viewDidAppear 中,我添加类似于以下的逻辑:

if PFUser.currentUser() != nil{
    //perform segue
    //or present view controller modally
}

将登录 View Controller 置于层次结构中非常重要,例如注销,这样您就可以放松到登录 View Controller 。

此外,有必要在 viewDidAppear 中执行 segue,因为只有在要转换的 View Controller 已添加到层次结构之后,您才必须执行 View Controller 转换。

关于ios - 如何使用 PFUser 更改初始 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230315/

相关文章:

iOS iPhone - 在 keyboardWillShow 之前获取 UIKeyboard 高度

ios - 如何判断UIScrollView要滚动的方向?

ios - 我们可以在 native iOS 应用程序中创建 localhost

ios - 更新 uitableview 标题上的文本

ios - 使用生成功能为图像添加动画

ios - 为 swift 开发的 ios 应用程序检索存储为 PFFile 的视频文件

android - 检查本地数据存储是否为空

ios - 多个命令在新的 Firestore 版本中产生 gRPCCertificates.bundle 错误

ios - 无法在 Swift 中获取最新的 CoreData DB 文件

javascript - 如何在 Parse Cloud Code 中获取对象