UIActionSheet在iOS 7,SDK 7中关闭后,键盘隐藏并再次显示

前端之家收集整理的这篇文章主要介绍了UIActionSheet在iOS 7,SDK 7中关闭后,键盘隐藏并再次显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在ViewController中创建一个UIActionSheet.我还添加代码来捕获UIKeyboardWillShowNotification和UIKeyboardWillHideNotification通知.

我的问题是当我解雇,我得到两个通知键盘隐藏和再次显示.
有人可以告诉我如何防止这个问题?它只发生在iOS 7中,并使用SDK 7构建

更新一些代码

在viewDidLoad中,我初始化一个按钮,当触摸按钮,操作表将被显示.

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5. UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  6. button.frame = CGRectMake(10,50,100,30);
  7. [button setTitle:@"Open menu" forState:UIControlStateNormal];
  8. [button addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
  9. [self.view addSubview:button];
  10.  
  11. UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(0,40)];
  12. [self.view addSubview:textView];
  13. [textView becomeFirstResponder];
  14.  
  15. [[NSNotificationCenter defaultCenter] addObserver:self
  16. selector:@selector(keyboardWillHide:)
  17. name:UIKeyboardWillHideNotification
  18. object:nil];
  19.  
  20. [[NSNotificationCenter defaultCenter] addObserver:self
  21. selector:@selector(keyboardWillShow:)
  22. name:UIKeyboardWillShowNotification
  23. object:nil];
  24. }
  25.  
  26. - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{
  27. [searchBar resignFirstResponder];
  28. }
  29.  
  30. - (void) buttonTouched{
  31. UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Action sheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive" otherButtonTitles:@"Hello",nil];
  32. [actionSheet showInView:self.view];
  33. }
  34.  
  35. - (void)keyboardWillShow:(NSNotification*)notification{
  36. NSLog(@"keyboardWillShow");
  37. }
  38.  
  39. - (void)keyboardWillHide:(NSNotification*)notification{
  40. NSLog(@"keyboardWillHide");
  41. }

我运行应用程序,键盘显示,我触摸按钮,动作表显示.我通过触摸其上的任何按钮关闭操作表,并记录打印:

keyboardWillShow

keyboardWillHide

keyboardWillShow

解决方法

有一个非常简单的解决方案.应该在控制器的.m文件添加私人本地类别
  1. @interface UIActionSheet (NonFirstResponder)
  2. @end
  3.  
  4. @implementation UIActionSheet (NonFirstResponder)
  5. - (BOOL)canBecomeFirstResponder
  6. {
  7. return NO;
  8. }
  9. @end

只有一个副作用.您的texField / textView在操作列表中保留焦点.但这不是我想的一大麻烦.

也可以以相同的方式对UIActionSheet进行子类化.

猜你在找的iOS相关文章