错误:无法将“FirstApp.ChoosePageViewController”类型的值转换为“FirstApp.CreateAccountPageViewController”

时间:2015-05-28 01:17:52

标签: ios swift viewcontroller

我正在使用Xcode 6.3.2创建单个视图应用。

我进行了设置,以便第一个视图控制器是一个主页(图1),当它出现时,它会自动进入登录/注册/访客页面。 (图2.)

从那里,如果有人选择注册,它会将它们带到一个快速屏幕(图3),这只是一个背景视图,用于检查注册是否有效。在该屏幕上,它会自动显示注册页面。 (图4.)

然后,如果注册完成或成功,注册页面将被解除回到该检查屏幕,然后将其解除,返回登录/注册/访客页面。

无法添加多个链接,因此我上传了项目的zip文件夹以及4张图片。

我的项目: http://www.mediafire.com/download/dnv1vqlx3j741zv/FirstApp.zip

一切正常,注册被取消。如果注册被取消,它将返回检查屏幕,然后就会卡在那里。我环顾四周,找不到任何有效的方法来解决问题,遗憾的是我无法从错误信息中找出解决方案。感谢任何帮助过的人!

我的错误讯息: 无法将'FirstApp.ChoosePageViewController'(0x101edd680)类型的值转换为'FirstApp.CreateAccountPageViewController'(0x101edd380)。 (lldb)

登录/注册/访客页面视图控制器:

import UIKit

class ChoosePageViewController: UIViewController {

//Sets Button Outlets to Edit Displays
@IBOutlet weak var viewLoginButton: UIButton!
@IBOutlet weak var viewRegisterButton: UIButton!
@IBOutlet weak var viewGuestButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    viewLoginButton.layer.cornerRadius = 5.0
    viewRegisterButton.layer.cornerRadius = 5.0
    viewGuestButton.layer.cornerRadius = 5.0
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

注册检查页面视图控制器:

import UIKit

class RegistrationCheckPageViewController: UIViewController, DetailsDelegate {

@IBOutlet weak var viewRegistrationStatusLabel: UILabel!
@IBOutlet weak var viewRegistrationForceQuitLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//Allow CreateAccountPage to Edit RegistrationCheckPage Labels
func labelDelegateMethodWithString(RegistrationStatus: String, RegistrationForceQuit: String) {
    viewRegistrationStatusLabel.text! = RegistrationStatus
    viewRegistrationForceQuitLabel.text! = RegistrationForceQuit
}

//Prepare RegistrationCheckPage Controller for CreateAccountPage Controller Takeover
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    let controller = segue.destinationViewController as! CreateAccountPageViewController
    controller.delegate = self
}

//Immediately Present Create Account Page, Dismiss When Account is Created
//Display Error and Return to Create Account Page if Account Creation Fails Unexpectedly
override func viewDidAppear(animated: Bool) {

    if(viewRegistrationStatusLabel.text == "Complete!") {
        self.performSegueWithIdentifier("toChoosePageFromRegistrationCheckPage", sender: self)
    } else if(viewRegistrationForceQuitLabel.text == "Active") {
        displayErrorAlert("Account not created.")
        self.performSegueWithIdentifier("toChoosePageFromRegistrationCheckPage", sender: self)
    } else if(viewRegistrationStatusLabel.text == "Checking..." && viewRegistrationForceQuitLabel.text == "Inactive"){
    } else {
        displayErrorAlert("An unknown error occurred.")
    }

    self.performSegueWithIdentifier("toCreateAccountPage", sender: self)
}

func displayErrorAlert(displayMessage: String) {
    var myAlert = UIAlertController(title: "Error", message: displayMessage, preferredStyle: UIAlertControllerStyle.Alert)
    let dismissAction = UIAlertAction(title: "Retry", style: UIAlertActionStyle.Default, handler: nil)
    myAlert.addAction(dismissAction)
    self.presentViewController(myAlert, animated: true, completion: nil)
}

/*
// 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.
}
*/

}

创建帐户页面视图控制器:

import UIKit

//Allows RegistrationCheckPage and CreateAccountPage to Communicate
protocol DetailsDelegate {
    func labelDelegateMethodWithString(RegistrationStatus: String, RegistrationForceQuit: String)
}

class CreateAccountPageViewController: UIViewController {

//Sets Button Outlets to Edit Displays
@IBOutlet weak var viewCreateButton: UIButton!
@IBOutlet weak var viewCancelButton: UIButton!

var registrationStatus = "Checking..."
var registrationForceQuit = "Inactive"

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    viewCreateButton.layer.cornerRadius = 5.0
    viewCancelButton.layer.cornerRadius = 5.0
}

//CreateAccountPage Allows or Disallows RegistrationCheckPage to Continue
var delegate: DetailsDelegate!
override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    delegate.labelDelegateMethodWithString(registrationStatus, RegistrationForceQuit: registrationForceQuit)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var userUsernameTextField: UITextField!
@IBOutlet weak var userEmailTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!
@IBOutlet weak var userRepeatPasswordTextField: UITextField!

@IBAction func didPressCreateAccountButton(sender: AnyObject) {

    let userUsername = userUsernameTextField.text;
    let userEmail = userEmailTextField.text;
    let userPassword = userPasswordTextField.text;
    let userPasswordRepeat = userRepeatPasswordTextField.text;

    func displayAlert(displayMessage:String) {
        var myAlert = UIAlertController(title: "Wait!", message:displayMessage, preferredStyle: UIAlertControllerStyle.Alert)
        let dismissAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
        myAlert.addAction(dismissAction)
        self.presentViewController(myAlert, animated: true, completion: nil)
    }

    //Check for Empty Fields
    if(userUsername.isEmpty || userEmail.isEmpty || userPassword.isEmpty || userPasswordRepeat.isEmpty) {
        displayAlert("All fields are required!")
        return;
    }

    //Make Sure Username Is Not In Use
    /*if(username is in use) {
    displayAlert("Username is already in use.")
    return;
    }*/

    //Make Sure Passwords Match
    if(userPassword != userPasswordRepeat) {
        displayAlert("The passwords did not match!")
        return;
    }

    //Store Data


    //Successful Registration
    registrationStatus = "Complete!"
    registrationForceQuit = "Inactive"
    delegate.labelDelegateMethodWithString(registrationStatus, RegistrationForceQuit: registrationForceQuit)
    self.dismissViewControllerAnimated(true, completion: nil)
}

//Cancel the Registration Without Finishing
@IBAction func didPressCancelRegistrationButton(sender: AnyObject) {
    registrationStatus = "Failed"
    registrationForceQuit = "Active"
    delegate.labelDelegateMethodWithString(registrationStatus, RegistrationForceQuit: registrationForceQuit)
    self.dismissViewControllerAnimated(true, completion: nil)
}

/*
// 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.
}
*/

}

1 个答案:

答案 0 :(得分:2)

在这一行:

let controller = segue.destinationViewController as! CreateAccountPageViewController

您的segue.destinationViewController不是您的预期,实际上是ChoosePageViewController

您有两个分段,"toChoosePageFromRegistrationCheckPage""toCreateAccountPage"

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    let controller = segue.destinationViewController as! CreateAccountPageViewController
    controller.delegate = self
}

就在这里,您必须检查segue.identifier是否等于"toChoosePageFromRegistrationCheckPage""toCreateAccountPage",并根据标识符将目标投射到相应的视图控制器。