我从这个网站获得了教程:http://jamesleist.com/ios-swift-tutorial-stop-segue-show-alert-text-box-empty/
这是我目前的代码。它只是不起作用,导致应用程序崩溃,因为如果任何字段为空,则下一个ViewController崩溃。
import Foundation
import UIKit
import Darwin
class View3on3 : UIViewController, UITextFieldDelegate {
@IBOutlet weak var APTeams: UITextField!
@IBOutlet weak var APRounds: UITextField!
@IBOutlet weak var APBreakers: UITextField!
var AP1: String = String()
var AP2: String = String()
var AP3: String = String()
override func viewDidLoad()
{
super.viewDidLoad()
initializeTextFields()
}
func initializeTextFields()
{
APTeams.delegate = self
APTeams.keyboardType = UIKeyboardType.NumberPad
APRounds.delegate = self
APRounds.keyboardType = UIKeyboardType.NumberPad
APBreakers.delegate = self
APBreakers.keyboardType = UIKeyboardType.NumberPad
}
@IBAction func userTappedBackground(sender: AnyObject)
{
view.endEditing(true)
}
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
if identifier == "segueTest" {
if (APTeams.text!.isEmpty) {
let alert = UIAlertView()
alert.title = "No Text"
alert.message = "Please Enter Text In The Box"
alert.addButtonWithTitle("Ok")
alert.show()
return false
}
else {
return true
}
}
// by default, transition
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueTest"){
var passs = segue.destinationViewController as! View3on3Results
passs.AP1 = APTeams.text!
passs.AP2 = APRounds.text!
passs.AP3 = APBreakers.text!
}
}
}
我尝试在SO上查看其他问题,但解决方案都出错了。我认为这是由于Swift更新到2.0。我可能是错的 - 这是非常新的。
答案 0 :(得分:0)
我希望可能是因为AlertView。在swift 2.0中,UIAlertController取代了UIAlertView:
let alertController: UIAlertController = UIAlertController(title: "Hello", message: "Are you sure?", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)
使用上面的代码将UIAlertView替换为UIAlertViewController。
编辑:
同时制作IBOutlet选项:
@IBOutlet weak var APTeams: UITextField?
@IBOutlet weak var APRounds: UITextField?
@IBOutlet weak var APBreakers: UITextField?
答案 1 :(得分:0)
我认为如果segue
不是performSegueWithIdentifier
,那么应该使用textfield
手动拨打empty
,如果是alert
您的代码可能看起来像[在提交您的数据时可能会点击按钮,或者尝试在<{1}}之后或将此代码置于viewDidLoad
内}
initializeTextFields()
如果我没错,你就不需要if(APTeams.text!.isEmpty){
// your code for showing alert. You can use UIAlertController if UIAlertView is not working.
}
else
{
self.performSegueWithIdentifier("segueTest",sender: View3on3Results.self)
}
。
答案 2 :(得分:0)
解决!这是有效的代码。感谢@SohilRMemon和@FrequencyMatched帮助我把它放在一起。
代码在这里:
import Foundation
import UIKit
import Darwin
class View3on3 : UIViewController, UITextFieldDelegate {
@IBOutlet weak var APTeams: UITextField!
@IBOutlet weak var APRounds: UITextField!
@IBOutlet weak var APBreakers: UITextField!
var AP1: String = String()
var AP2: String = String()
var AP3: String = String()
override func viewDidLoad()
{
super.viewDidLoad()
initializeTextFields()
}
func initializeTextFields()
{
APTeams.delegate = self
APTeams.keyboardType = UIKeyboardType.NumberPad
APRounds.delegate = self
APRounds.keyboardType = UIKeyboardType.NumberPad
APBreakers.delegate = self
APBreakers.keyboardType = UIKeyboardType.NumberPad
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (APTeams.text!.isEmpty || APRounds.text!.isEmpty || APBreakers.text!.isEmpty)
{
let alertController: UIAlertController = UIAlertController(title: "Hello", message: "Are you sure?", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)
}
else
{
let DestViewController : View3on3Results = segue.destinationViewController as! View3on3Results
DestViewController.AP1 = APTeams.text!
DestViewController.AP2 = APRounds.text!
DestViewController.AP3 = APBreakers.text!
//self.performSegueWithIdentifier("segueTest",sender: View3on3Results.self)//
}
}
}