我在ViewController中创建了一个 UIActionSheet 。我还添加了代码来捕获 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 通知。
我的问题是当我解雇时,我得到两个通知键盘隐藏并再次显示。 有人可以告诉我如何预防这个问题?它只发生在iOS 7中并使用SDK 7构建
更新一些代码:
在viewDidLoad中,我初始化一个按钮,当触摸按钮时,将显示动作表。
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 50, 100, 30);
[button setTitle:@"Open menu" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[self.view addSubview:textView];
[textView becomeFirstResponder];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{
[searchBar resignFirstResponder];
}
- (void) buttonTouched{
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Action sheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive" otherButtonTitles:@"Hello", nil];
[actionSheet showInView:self.view];
}
- (void)keyboardWillShow:(NSNotification*)notification{
NSLog(@"keyboardWillShow");
}
- (void)keyboardWillHide:(NSNotification*)notification{
NSLog(@"keyboardWillHide");
}
我运行app,键盘会显示,我触摸按钮,动作表显示。我通过触摸任何按钮关闭动作表,然后记录打印:
keyboardWillShow
keyboardWillHide
keyboardWillShow
答案 0 :(得分:24)
有一个非常简单的解决方案。应该在控制器的.m文件中添加私有本地类别
@interface UIActionSheet (NonFirstResponder)
@end
@implementation UIActionSheet (NonFirstResponder)
- (BOOL)canBecomeFirstResponder
{
return NO;
}
@end
它只有一个副作用。您的texField / textView在活动表演示期间保持关注。但我认为这不是一个大麻烦。
也可以用同样的方式继承 UIActionSheet 。
答案 1 :(得分:0)
工作正常。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(textField==myTextField2){
[myTextField1 resignFirstResponder];
[self showActionSheet];
return NO;
}
return YES;
}