UITextField に入力した後に何かを作成する UITextField のメソッドを探しています。
ボタンを使用して計算を行うメソッドがありますが、使用したくありません。UITextField に数値を入力した後で、このメソッドを実行してもらいたいです。
どの方法を使用するかについてのヒントはありますか?
助けてくれてありがとう。
UITextField に入力した後に何かを作成する UITextField のメソッドを探しています。
ボタンを使用して計算を行うメソッドがありますが、使用したくありません。UITextField に数値を入力した後で、このメソッドを実行してもらいたいです。
どの方法を使用するかについてのヒントはありますか?
助けてくれてありがとう。
UITextFieldFieldDelegate
プロトコルを実装するself.textField.delegate = self;
shouldChangeCharactersInRange
次に、メソッドを実装します
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self doCalculations];
return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
入力したテキストをすぐに取得するために使用できます。または- (BOOL)textFieldShouldReturn:(UITextField *)textField
、ユーザーがリターンを押したときにキャプチャし、その時点でテキスト フィールドからテキストを読み取るために使用できます。これらのメソッドはどちらも UITextFieldDelegate プロトコルにあり、そのドキュメントはこちらにあります。
編集:
または、textChanged メソッドを使用[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
して実装し、EditingChanged イベントをキャプチャすることもできます。
はい、このコードを使用して行うことができます:-
このコードは、指定されたテキストを変更する必要があるかどうかをデリゲートに尋ねます。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self yourMethodName]; //The method which you want to call.
return YES;
}
UITextFieldFieldデリゲートを呼び出します。これをよりよく理解するために、これを参照できます:-
これがあなたを助けることを願っています。
//in your ViewContoller.h
//Implement UITextFieldFieldDelegate protocol
//In your ViewContoller.m
//where you are creating your textfield.
textField.delegate = self;
// There are 2 delegate method that you can implement
// this method will be called whwn ever you enter a a letter.
// say you enter 'abcd' then this method will be called 4 times.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//do your calculation if required
return YES;
}
//this method will be called when the textField will end editing, i.e when the keyboard resigns of the control goes to some other textField/textView
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
要件に基づいて、1 番目または 2 番目の方法で計算を行うことができます。