Interrupt a chain of actions in SWIFT

0

I am developing a simple application with two screens, authentication uama and another for a second process, this application is for IOS using the SWIFT language.

When using the Storebord to set the screen sequences I came across the situation where there are two actions (Events) that must be executed, one is called "Touch Up Inside" that I defined that call the function defined below as @IBAction func doLogin(sender: UIButton) and second that proves the screen sequence and is an internal function.

As I am a beginner in the language before trying another approach I would like to know how I should proceed to intercept and avoid executing the next action (the change of screen), if the data entered is not valid this will not be the screen transition.

I will post below the code I have of the main Controller that I am using for both screens, see that there are two functions, the first one would be the function responsible for taking care of the login process ( @IBAction func doLogin(sender: UIButton) ), after this code is the XML that defines the storyboard, being just the part of the login button.

//
//  ViewController.swift
//  Minha Primeira Interface
//
//  Created by Carlos Delfino on 17/09/15.
//  Copyright © 2015 Carlos Delfino. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    // MARK: properties
    @IBOutlet weak var loginTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var consultDocumentButton: UIButton!


    // MARK: unknow
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    // MARK: Actions
    @IBAction func doLogin(sender: UIButton) {

    }

    @IBAction func doConsultDocument(sender: UIButton){

    }


}

Below is the XML for the part of the StoryBoard that defines the Login button:

...
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fAu-9t-LKt">
   <rect key="frame" x="277" y="503" width="46" height="30"/>
   <size key="titleShadowOffset" width="15" height="11"/>
   <state key="normal" title="Login">
        <color key="titleColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
   </state>
   <variation key="widthClass=compact" fixedFrame="YES">
        <rect key="frame" x="115" y="477" width="170" height="80"/>
   </variation>

   <connections>
        <action selector="doLogin:" destination="BYZ-38-t0r" eventType="touchUpInside" id="BMZ-tf-EZp"/>
        <segue destination="dEo-UW-BIk" kind="show" id="QdU-ZB-kVG"/>
   </connections>

</button>
...
    
asked by anonymous 18.09.2015 / 22:44

1 answer

2

Since you have used storyboard itself to make connections between screens, you need to treat the rule in shouldPerformSegueWithIdentifier

SWIFT2

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {

    if (textField.loginTextField!.characters.count == 0 || textField.passwordTextField!.characters.count == 0) {
        //Aqui você adiciona sua lógica para avisar o usuário
        return false;
    }

    return true;

}

If you want to pass some value to the next screen, you can use prepareForSegue

If you use multiple screen connections for storyboard you need to check which identifier in shouldPerformSegueWithIdentifier otherwise the rule will work for everyone

    
18.09.2015 / 23:19