简体   繁体   English

如何在某些情况下禁用按钮操作

[英]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. 动作是我使用拖放方式创建的segue。 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? 如果不能,那么在这种情况下以编程方式进行segue的最可接受的方法是哪种?

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 "). 您应该从按钮断开segue(删除segue),然后从loginController重新连接到nextController并添加segue的标识符(“ nextFlowId ”)。 And then add action to button ( loginAction ) 然后将动作添加到按钮( loginAction

Example: 例:

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

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

Add this method to your viewcontroller. 将此方法添加到您的viewcontroller。 Then check your username and password first after return YES. 然后,在返回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 然后手动推送到ViewController

From Apple's UIKit Framework Reference: 从Apple的UIKit框架参考中:

userInteractionEnabled 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. 设置为NO时,将忽略用于视图的用户事件(例如触摸和键盘)并将其从事件队列中删除。 When set to YES, events are delivered to the view normally. 设置为YES时,事件将正常传递到视图。 The default value of this property is YES. 此属性的默认值为YES。

UIButton inherits this property from UIView. UIButton从UIView继承此属性。 So you can set 所以你可以设置

aButton.userInteractionEnabled = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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