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

我在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,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");
}

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

keyboardWillShow

keyboardWillHide

keyboardWillShow

解决方法

一个非常简单的解决方案.应该在控制器的.m文件添加私人本地类别
@interface UIActionSheet (NonFirstResponder)
@end

@implementation UIActionSheet (NonFirstResponder)
- (BOOL)canBecomeFirstResponder
{
    return NO;
}
@end

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

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...