こんにちは、アプリケーションに 2 つの UITextFields があり、UITextFields 以外の場所をタッチしたときにキーボードを閉じたいのですが、どうすればよいですか?
10 に答える
この目的のために tochesBegin メソッドを使用します
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[yourFirstTextField resignFirstResponder];
[yourSecondTextField resignFirstResponder];
}
他に何も必要ありません。ビューのどこかをタッチすると、両方の textField が辞任し、どちらがフォーカスを持っているかを知る必要がなくなります。
イシュの答えは良いものです。しかし、次のことも試してみるべきだと思います。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
バックグラウンドでカスタム ボタンを作成してから、それIBAction
を呼び出すメソッドに接続する必要resignFirstResponder
があります。UITextField
このようなもの
編集:
このコードを使用するよりもプログラムでボタンを追加したいので
- (void)viewDidLoad
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
button.buttonType = UIButtonTypeCustom;
[button addTarget:self action:(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button release];
この後にコントローラーコードを追加します。
目に見えないボタンを作成し、ボタンの後ろに配置して、ボタンがタップされたときにテキスト フィールドにメッセージをUITextField
送信します。resignFirstResponder
デリゲートを使用し、
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
コントローラーでメソッドを作成することもできます
-(IBAction)editingEnded:(id)sender{
[sender resignFirstResponder];
}
次に、IB接続の接続インスペクターで、「終了時に終了しました」というイベントをそれに接続します。
通常、このシナリオでは、残りのユーザー インターフェイスを保持するスクロール ビューが表示されます。そのスクロール ビューのデリゲートをビュー コントローラーに割り当て、 protocol を実装すると言う場合UIScrollViewDelegate
、次のメソッドを追加するだけで機能します。
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
Interface builder を使用していない場合は、ここで説明されているように手動でイベントをフックできます: How can I link an UIView or UIImageView with an event like "touch up inside"?
Interface builder を使用している場合は、背景ボタンを作成する必要はありません。
あなたはこれを行うことができます:
- ビューを選択します (おそらくビューと呼ばれます)
- プロパティ インスペクターの (i) タブに変更します。
- クラスを UIView から UIControl に変更します (これにより、 Touch Downイベントにアクセスできるようになります)。
- Touch Downイベントを backgroundTap メソッドにフックします (Ctrl + ドラッグ)。
- 変更を保存することを忘れないでください
これを使って。
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 180; distace of keyboard up.
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
You need to define the action for your button click this way
[infoButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
And implement the click method ,as for example i have provided the animation on button click method.
-(void)backButtonClicked
{
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[self.navigationController popViewControllerAnimated:YES];
[UIView commitAnimations];
}
Hope this will help you ,the action is without using IB.Its all done through coding
ビュー コントローラーで UITextField への参照を保持し、[yourTextField rejectFirstResponder] を呼び出します。