Interrupt a chain of actions on SWIFT

Asked

Viewed 72 times

0

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

When using Storebord to define 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 to call the function defined below as @IBAction func doLogin(sender: UIButton) and second that proves the sequence of screen and is an internal function.

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

I will post below the code I have from the main Controller that I am using for both screens, see that there are two functions the first would be the function responsible for taking care of the login process (@IBAction func doLogin(sender: UIButton)), right after this code is the XML that defines the storyboard, being only 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 Storyboard that defines the button Login:

...
<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>
...

1 answer

2


How you used your own storyboard to make connections between the screens, you need to treat the rule on 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 the prepareForSegue

Obs: If you use multiple screen connections by storyboard you need to verify which identifier on shouldPerformSegueWithIdentifier otherwise the rule will work for everyone

  • what a show!!! Thank you Jeferson.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.