无法将值类型'TableViewController.Type'转换为预期的参数类型'UIViewController'

时间:2015-10-18 14:18:18

标签: ios swift uiviewcontroller swift2

我正在制作一个注册并登录的应用程序。我为此使用了Parse。如果登录/注册成功,我想将View Controller移动到TableViewController。但是我收到此错误:“无法将值类型'TableViewController.Type'转换为期望的参数类型'UIViewController'。

以下是我收到错误的行:

func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {
    self.dismissViewControllerAnimated(true, completion: nil)
    self.presentViewController(TimelineTableViewController, animated: true, completion: nil)


}

以下是整个代码:

//  Created by Ojas Sethi on 12/10/15.
//  Copyright © 2015 Jell Apps. All rights reserved.
//

import UIKit
import Parse
import ParseUI

class LoginSignupViewController: UIViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {
    var logInViewController: PFLogInViewController = PFLogInViewController()
    var signUpViewController: PFSignUpViewController = PFSignUpViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        if (PFUser.currentUser() == nil){
            self.logInViewController.fields = PFLogInFields.UsernameAndPassword
            self.logInViewController.fields = PFLogInFields.LogInButton
            self.logInViewController.fields = PFLogInFields.SignUpButton
            self.logInViewController.fields = PFLogInFields.PasswordForgotten
            self.logInViewController.fields = PFLogInFields.DismissButton

            /*| PFLogInFields.LogInButton | PFLogInFields.SignUpButton | PFLogInFields.PasswordForgotten | PFLogInFields.DismissButton*/

            let logoInLogoTitle = UILabel()
            logoInLogoTitle.text = "Ziffer"
            logoInLogoTitle.font = UIFont.systemFontOfSize(25)

self.logInViewController.logInView?.logo = logoInLogoTitle
            self.logInViewController.delegate = self

            let signUpLogoTitle = UILabel()
            signUpLogoTitle.text = "Ziffer"
            logoInLogoTitle.font = UIFont.systemFontOfSize(25)

            self.signUpViewController.signUpView?.logo = signUpLogoTitle
            self.signUpViewController.delegate = self
            self.logInViewController.signUpController = self.signUpViewController
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool {
        if (!username.isEmpty || !password.isEmpty){
            return true
        }else{
            return false
        }
    }

    func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {
        self.dismissViewControllerAnimated(true, completion: nil)
        self.presentViewController(TimelineTableViewController, animated: true, completion: nil)
    }

    func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?) {
        print("failed to login")
    }

    func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser) {
        self.dismissViewControllerAnimated(true, completion: nil)
        self.presentViewController(logInViewController, animated: true, completion: nil)
        SignUpSuccessfulAlert()

    }

    func signUpViewController(signUpController: PFSignUpViewController, didFailToSignUpWithError error: NSError?) {
        print("Failed to signup...")

        SignUpFaliedAlert()
    }

    func signUpViewControllerDidCancelSignUp(signUpController: PFSignUpViewController) {
        print("User dismissed sign up.")
    }

    func SignUpSuccessfulAlert(){
        var alertController : UIAlertController
    alertController = UIAlertController(title: "Sign Up Successful", message: "Yay! Sign up was successful! Now you can start using Ziffer!", preferredStyle: .Alert)

        let doneAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)

        alertController.addAction(doneAction)

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

    func SignUpFaliedAlert(){
        var signUpalertFail : UIAlertController

        signUpalertFail = UIAlertController(title: "Failed", message: "Sorry! Sign up faield. Check the connections and try again later", preferredStyle: .Alert)

        let okAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)

        signUpalertFail.addAction(okAction)

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

2 个答案:

答案 0 :(得分:4)

首先,您正试图从即将解散的登录视图控制器中呈现您的新视图控制器。那是不对的。您可能希望从呈现登录视图控制器的稳定视图控制器呈现它。以下是关于如何执行此操作的nice example。这是基于objective-c所以请耐心等待。

其次,您需要创建一个TimelineTableViewController的对象才能显示在视图层次结构中(再次查看我上面分享的链接)。像这样:

let timeLineTableVC = TimelineTableViewController()
self.presentViewController(timeLineTableVC, animated: true, completion: nil)

答案 1 :(得分:3)

您的(大写)TimelineTableViewController似乎是,而不是类实例。您需要先创建此控制器的实例。

建议的方法是在故事板中为此控制器创建一个segue,然后在想要显示控制器时调用performSegueWithIdentifier

与您的代码最接近的是使用instantiateViewControllerWithIdentifier在代码中实例化视图控制器(再次:来自故事板),但上面的方法代码较少,做同样的事情,因此是首选。< / p>