不能将值类型'TableViewController.Type'转换为期望的参数类型'UIViewController'
问题描述:
我正在注册并登录应用程序。我为此使用了Parse。如果登录/注册成功,我想将View Controller移动到TableViewController。但是我收到此错误:“无法转换值type'TableViewController.Type”预期参数类型‘的UIViewController’不能将值类型'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)
}
}
答
你(大写)TimelineTableViewController
似乎是一个类,而不是一个类的实例,您需要首先创建该控制器的一个实例
推荐的方法是在故事板中为此控制器创建一个segue,然后在想要呈现控制器时调用performSegueWithIdentifier
。
与代码最接近的地方是在instantiateViewControllerWithIdentifier
的代码中实例化视图控制器的代码(同样来自故事板),但上面的方法代码少,功能相同,因此是首选。
答
首先,您正试图从登录视图控制器中呈现您即将关闭的新视图控制器。那是不对的。你会想要从提供登录视图控制器的稳定视图控制器中呈现它。这里是关于如何做到这一点的nice example。这是基于objective-c的,因此要耐心。
其次,您需要创建一个对象TimelineTableViewController
以呈现在视图层次结构中(再次查看我在上面共享的链接)。像这样的东西:
let timeLineTableVC = TimelineTableViewController()
self.presentViewController(timeLineTableVC, animated: true, completion: nil)
非常感谢你帮助我! – ojassethi
嗯...... Mundi,当我实例化视图控制器时,它只是给了我一部分TableViewController。它不会给我像按钮,文本域等UI元素 – ojassethi