1
在我的iPhone應用程序中,我將UITextView添加到了我的主視圖中,並且我想在用戶單擊UITextView邊界之外的屏幕中的任何位置時將其解除。當用戶在外面點擊時關閉iOS中的UITextView
可能嗎?什麼是實施這個最好的方法?
在我的iPhone應用程序中,我將UITextView添加到了我的主視圖中,並且我想在用戶單擊UITextView邊界之外的屏幕中的任何位置時將其解除。當用戶在外面點擊時關閉iOS中的UITextView
可能嗎?什麼是實施這個最好的方法?
嘗試使用此
-(void)ViewDidLoad{
....
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer: tapRec];
....
}
-(void)tap:(UITapGestureRecognizer *)tapRec{
[[self view] endEditing: YES];
}
您還可以使用即可選
[textView resignFirstResponder];
在UITapGestureRecognizer
-(void)tap:(UITapGestureRecognizer *)tapRec{
[yourTextView resignFirstResponder];
}
,或者如果需要在一下旁邊然後
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
我建議把你的mainView變成UIControl。
-(IBAction)backgroundTap:(id)sender
{
[[self view] endEditing:YES];
}
這是否解決問題烏爾 –