我正在使用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.
}
*/
}
最佳答案
在这一行:
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"
,并根据标识符将目标转换为适当的视图控制器。关于ios - 错误:无法将类型“FirstApp.ChoosePageViewController”的值强制转换为“FirstApp.CreateAccountPageViewController”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30495657/