简体   繁体   中英

Cancel Segue and show alert - Xcode

I got the tutorial from this site: http://jamesleist.com/ios-swift-tutorial-stop-segue-show-alert-text-box-empty/

This is my current code. It's just not working, which causes the app to crash because if any of the fields are empty, the next ViewController crashes.

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!
        }


}
}

I tried looking at other questions on SO, but the solutions all threw errors. I think this is due to Swift updating to 2.0. I could be wrong- very new to this.

I hope it may be because of AlertView. In swift 2.0 UIAlertView is replaced by UIAlertController:

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)

Replace your UIAlertView with UIAlertViewController using the above code.

Edited:

Also make the IBOutlet optionals:

@IBOutlet weak var APTeams: UITextField?
@IBOutlet weak var APRounds: UITextField?
@IBOutlet weak var APBreakers: UITextField?

I think you should call segue manually using performSegueWithIdentifier if the textfield is not empty and if it is then show alert

Your code might look like [may be on button click when you are submitting your data or trying to segue Or put this code inside viewDidLoad after 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)

}

And you dont need shouldPerformSegueWithIdentifier if I'm not wrong.

Solved! This was the code that worked. Thank you to @SohilRMemon and @FrequencyMatched for helping me put this together.

Code here:

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)//
        }



        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM