Faruk
Faruk

Reputation: 2449

How to disable button action under certain conditions

I want to disable button action under certain conditions. To be more clear, I am developing a login page but i want to disable login button action. The action is a segue which I created using drop&drag way. Question is How can I disable that segue when username and password are invalid. Or can it be done by this way?

If it can't, which way is the most acceptable to do a segue programmatically in this situation?

Upvotes: 0

Views: 426

Answers (3)

disepulv
disepulv

Reputation: 252

If I understand you connect your login button directly to next controller. The problem with that is you can´t add logic for segue.

You should disconnect segue from button (delete segue) and reconnect from loginController to nextController and add an identifier for segue ( "nextFlowId"). And then add action to button (loginAction)

Example:

   @IBAction func loginAction(sender: AnyObject) {
       if yourCondition {
           //message and return
           return
       }

       self.performSegueWithIdentifier("nextFlowId", sender: nil)
   }

Upvotes: 1

Yakiv Kovalskyi
Yakiv Kovalskyi

Reputation: 1757

From Apple's UIKit Framework Reference:

userInteractionEnabled

A Boolean value that determines whether user events are ignored and removed from the event queue.

Discussion

When set to NO, user events — such as touch and keyboard — intended for the view are ignored and removed from the event queue. When set to YES, events are delivered to the view normally. The default value of this property is YES.

UIButton inherits this property from UIView. So you can set

aButton.userInteractionEnabled = false

Upvotes: 0

Cong Tran
Cong Tran

Reputation: 1458

Add this method to your viewcontroller. Then check your username and password first after return YES.

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {

}

Or I think you can add touch up inside action of login button. Then push to viewcontroller manually

Upvotes: 0

Related Questions