2017-04-05 5 views
4

저는 textField의 말도 안되는 문제에 직면했습니다. 저는 tfA와 tfB라는 두 개의 텍스트 필드가 있습니다. 위임자와 그 텍스트 필드에 대해 모두 원하는대로 설정했습니다. 만약 내가 tfA를 클릭하면 뭔가를 인쇄해야하고, 인쇄 중이며 tfB를 클릭하면 키보드가 나타납니다. 잘 작동하고 있습니다.하지만 다시 tfA를 클릭하면 인쇄가됩니다. 거기 주어진 조건에 따라 해고하지만, 키보드도 거기에서 해고되지 않습니다 self.view.endEditing(true) 여기에서 작동하지 않습니다. 아래 코드는 스크린 샷과 함께 제공됩니다. 여기서 제가 잘못하고있는 것은 무엇입니까?UITextField의 resignFirstResponder가 작동하지 않습니다. self.view.endEditing()

CODE : 스위프트 3

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate { 
    @IBOutlet weak var tfA: UITextField! 
    @IBOutlet weak var tfB: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tfA.delegate = self 
     tfB.delegate = self 
    } 

    func textFieldDidBeginEditing(_ textField: UITextField) { 
     if textField == tfA{ 
      print("TFA Clicked") 
      textField.resignFirstResponder() 
      self.view.endEditing(true) 
     }else{ 
      tfB.becomeFirstResponder() 
     } 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 

     return true 
    } 

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


} 

스크린 샷

enter image description here

+0

제거'textField.resignFirstResponder을()'시도 : textFieldShouldBeginEditing로 교체, 당신의 textFieldDidBeginEditing 방법을 이동합니다. tfB에서 돌아 오면 키보드가 활성화됩니다. 그래서'self.view.endEditing (true)'는 뷰에서 활성화 된 키보드를 없애기에 충분하다. – ron27

+0

tfA를 편집 할 수 없도록하려는 이유는 무엇입니까? tfA.isEnabled = false'를 그냥 쓰지 마십시오. –

+0

@ ron27 시도했지만 작동하지는 않습니다. –

답변

6

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 
    if textField == tfA 
    { 
     self.view.endEditing(true) 
     tfaAction() 
     return false 
    } 
    else 
    { 
     return true 
    } 
} 
func tfaAction(){ 

} 
+0

노력에 감사드립니다.하지만 작동하지 않습니다. 내 문제는 tfB 키보드를 클릭하면 표시되고 tfA 키보드를 클릭하면 해제해야합니다. 또한 tfA를 처음 클릭하면 키보드가 표시되지 않아야합니다. 그러나 이것에서 tfA를 클릭하는 동작이 필요합니다. –

+0

완벽, 그 작업에 따라 ... 당신의 노력에 감사드립니다 .. –

4

다시 시도

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 
    if textField == tfA{ 
    print("TFA Clicked") 
    self.view.endEditing(true) 
    return false 
    }else{ 
    return true 
    } 
} 
+0

완벽하게, 그 노력에 따라 .. 당신의 노력에 감사드립니다 .. –