如何禁用UIBarButton,当他们在iOS中更改两个文本字段为空 - Swift 3

问题描述:

如果两个文本字段为空,我想禁用该栏按钮项目。 这两个文本框是textField和textFieldNbrSpans。如何禁用UIBarButton,当他们在iOS中更改两个文本字段为空 - Swift 3

只是一个文本框,我用下面的代码,它工作得很好:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    let oldText: NSString = textField.text! as NSString 
    let newText: NSString = oldText.replacingCharacters(in: range, with: string) as NSString 

    if (newText.length > 0) { 

     doneBarButton.isEnabled = (newText.length > 0) 
     let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 1.0) 
     let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!] 
     doneBarButton.setTitleTextAttributes(attrs, for: .normal) 

    } else { 

     let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 0.2) 
     let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!] 
     doneBarButton.setTitleTextAttributes(attrs, for: .normal) 
     doneBarButton.isEnabled = false 

    } 

当我试图另一个文本字段添加到上面的代码,它给了错误:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    let oldText: NSString = textField.text! as NSString 
    let newText: NSString = oldText.replacingCharacters(in: range, with: string) as NSString 

    let oldTextSpan: NSString = textFieldNbrSpans.text! as NSString 
    let newTextSpan: NSString = oldTextSpan.replacingCharacters(in: range, with: string) as NSString 

    if (newText.length > 0 && newTextSpan.length > 0) { 

     doneBarButton.isEnabled = (newText.length > 0 && newTextSpan.length > 0) 
     let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 1.0) 
     let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!] 
     doneBarButton.setTitleTextAttributes(attrs, for: .normal) 

    } else { 

     let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 0.2) 
     let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!] 
     doneBarButton.setTitleTextAttributes(attrs, for: .normal) 
     doneBarButton.isEnabled = false 

    } 

是有一种方法可以改进上面的代码,以便在2个文本框textfield & textfieldNbrSpan为空的情况下禁用栏按钮?

+0

什么是你得到的错误 –

这是你如何做到的。

  • 你应该有两个global变量。
  • 你应该定义tags你既text fields。要做到这一点,选择您的first textfield,去再下view section选择Tag0first text field和,1second textfield。和drag并为您的文本字段启用delegates(假设您知道这些)。
  • 创建和IBOutlet为您的navigation bar button

然后你view controller should look like. below.

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate { 

    @IBOutlet weak var mybutton: UIBarButtonItem! // outlet for your button 



    var textOne : NSString! = "" // global variable one to hold first text field value 
    var textTwo : NSString! = "" // global variable two to hold second text field value 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     mybutton.isEnabled = false // first your button should disabled 
    } 

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


    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // delegate method 

     if textField.tag == 0 { // get the first text field valued when user change the first text field 
      textOne = (textField.text ?? "") as NSString 
      textOne = textOne.replacingCharacters(in: range, with: string) as NSString! 
     } 
     else { // get the second text field value, when user change it 
      textTwo = (textField.text ?? "") as NSString 
      textTwo = textTwo.replacingCharacters(in: range, with: string) as NSString! 
     } 


     // check the two variables and do the enable and disable things. do your button styles also here. 
     if textOne != "" && textTwo != "" { 
      mybutton.isEnabled = true 
     } 
     else { 
      mybutton.isEnabled = false 
     } 

     return true 

    } 


} 

注:我并没有实施任何按钮样式,只是想告诉你如何做到这一点。希望这会对你有所帮助。祝你好运

+0

非常感谢,它按预期工作。 –

+0

欢迎您@XinLok。祝你好运 –