2016-10-05 5 views
-1
func textFieldShouldReturn(_ textfield: UITextField) -> Bool{ 
     p1s1TextField.resignFirstResponder() 
     return true 
} 

func textFieldShouldReturn(_ textfield: UITextField)-> Bool{ 
     p2s1TextField.resignFirstResponder() 
     return true 
} 

だからこれは私が、唯一の違いは、彼らは二つの異なるテキストフィールドに影響を与えるということで記述する必要があるコードです。再宣言のエラーを回避するために送信者を変更する必要があることを理解していますが、変更する必要があるかどうかは不明です。は二度同じ関数を呼び出す必要がありますが、「無効な再宣言」エラーを取得しています

+0

このメソッドのパラメータ( 'textField')を読む必要があります。あなたはできます( 'textField.resignFirstResponder()')。異なるユースケースの場合( 'if(textField == p1s1TextField){doSomethingSpecifictops1s1} else {}' – Larme

答えて

4

ただ1つを使用し、parameter textfieldは現在のtextfieldです。

ので:

func textFieldShouldReturn(_ textfield: UITextField) -> Bool { 
     textfield.resignFirstResponder() 
     return true 
} 
1

これは異なるテキストフィールドに従ってデリゲートメソッドを使用してみてください。

func textFieldShouldReturn(_ textfield: UITextField) -> Bool{ 
    if textfield == p1s1TextField { 
     p1s1TextField.resignFirstResponder() 
    }else if textfield == p2s1TextField { 
     p2s1TextField.resignFirstResponder() 
    } 
     return true 
} 
関連する問題