2017-02-26 6 views
0

2つのテキストフィールドが空の場合、バーボタンアイテムを無効にしたいとします。 2つのテキストフィールドはtextFieldとtextFieldNbrSpansです。ちょうど1つのTextFieldにiOSで2つのテキストフィールドが空であるときにUIBarButtonを無効にする方法 - 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 

    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 

    } 

は、次のとおりです。

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 

    } 

私は上記のコードに別のテキストフィールドを追加しようとしたとき、それはエラーを与えました2つのテキストフィールドtextfield & textfieldNbrSpanが空の場合、バーボタンを無効にする方法で上記のコードを改善する方法がありますか?

+0

あなたが持っているエラーは何ですか –

答えて

1

これはあなたがそれを行う方法です。

  • global変数が2つあります。
  • そして、あなたは、それを行うあなたのfirst textfieldを選択し、view sectionの下で、その後に行く.TOあなたの両方text fieldstagsを定義する必要がありますfirst text fieldため0としてTagを選択して、second textfieldため1dragを入力し、テキストフィールドにdelegatesを有効にします(これを知っていることを前提とします)。
  • の場合はIBOutletを作成してください。

次に、あなたの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ようこそ。がんばろう –

関連する問題