Present AlertController通过代码呈现的另一个ViewController

时间:2015-08-18 13:36:20

标签: ios swift

我通过空VC上的代码实例化Parse的股票UI登录屏幕。如果未验证用户的电子邮件,我想显示AlertController。所以我在我通过代码创建的logInViewController上呈现alertController ....我理解AlertController不在窗口层次结构中的错误,但我不确定如何解决它。它可以工作,如果我解雇logInViewController但我不想,我希望它存在于后台。使用Swift。

这个答案没有达到确切的问题:AlertController is not in the window hierarchy

override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)
        if (PFUser.currentUser() == nil) {

            //build logInVC in Code:
            var logInViewController = PFLogInViewController()

            var logInLogoTitle = UILabel()

            logInLogoTitle.text = "Thredz"

            logInLogoTitle.font = UIFont(name: "Cochin", size: 40.0)

            logInViewController.fields = PFLogInFields.UsernameAndPassword | PFLogInFields.LogInButton | PFLogInFields.SignUpButton | PFLogInFields.PasswordForgotten | PFLogInFields.Twitter

            logInViewController.logInView?.backgroundColor = UIColor.whiteColor()

            logInViewController.logInView?.logo = logInLogoTitle

            logInViewController.delegate = self

            //present log in VC
            self.presentViewController(logInViewController, animated: true, completion: nil)

            //build signUpViewController
            var signUpViewController = PFSignUpViewController()

            signUpViewController.delegate = self

            var signUpLogoTitle = UILabel()

            signUpLogoTitle.text = "Thredz"

            signUpViewController.signUpView?.logo = signUpLogoTitle

            logInViewController.signUpController = signUpViewController

        }


    }



  func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {


        if (PFTwitterUtils.isLinkedWithUser(user)) {
            var twitterUsername = PFTwitterUtils.twitter()?.screenName

            PFUser.currentUser()?.username = twitterUsername

            PFUser.currentUser()?.saveEventually(nil)

        }

        if user["emailVerified"] as! Bool == true {
            dispatch_async(dispatch_get_main_queue()) {
                self.performSegueWithIdentifier(self.peopleTableViewControllerSegue, sender: nil)
            }
        } else {
            // User needs to verify email address before continuing
            let alertController = UIAlertController(
                title: "Email address verification",
                message: "We have sent you an email that contains a link - you must click this link before you can continue.",
                preferredStyle: UIAlertControllerStyle.Alert
            )
            alertController.addAction(UIAlertAction(title: "OK",
                style: UIAlertActionStyle.Default,
                handler: nil)
            )

            self.presentViewController(alertController, animated: true, completion: nil)
        }

    }

1 个答案:

答案 0 :(得分:1)

logInViewController(logInController:didLogInUser:)功能中,更改行

self.presentViewController(alertController, animated: true, completion: nil)

logInController.presentViewController(alertController, animated: true, completion: nil)

这将在登录控制器顶部而不是空视图控制器上显示警报控制器。