本文介绍了在iOS 7,SDK 7中关闭UIActionSheet后,键盘会立即隐藏并再次显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ViewController中创建了一个 UIActionSheet 。我还添加了代码来捕获 UIKeyboardWillShowNotification UIKeyboardWillHideNotification 通知。

I create an UIActionSheet in my ViewController. I also add code to catch UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notification.

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

My problem is when I dismiss, I get two notification keyboard hide and show again.Somebody can show me how to prevent that problem ? It only happen in iOS 7 and build with SDK 7

更新一些代码:

在viewDidLoad中,我初始化一个按钮,当触摸按钮时,将显示动作表。

In viewDidLoad, I init a button, when touch button, action sheet will be showed.

- (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,键盘会显示,我触摸按钮,动作表显示。我通过触摸它上面的任何按钮来解除操作表,并且记录打印:

I run app, keyboard will showed, I touch button, action sheet showed. I dismiss action sheet by touch any button on it, and Log print :

keyboardWillHide

keyboardWillHide

keyboardWillShow

keyboardWillShow


推荐答案

有一个非常简单的解决方案。应该在控制器的.m文件中添加私有本地类别

There is a very simple solution. One should add private local category in .m file of the controller

@interface UIActionSheet (NonFirstResponder)
@end

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

它只有一个副作用。您的texField / textView在活动表演示期间保留焦点。但是我认为这不是一个大问题。

There is the only one side effect of it. Your texField/textView retains focus during action sheet presenting. But it is not a big trouble I think.

还可以用同样的方式对 UIActionSheet 进行子类化。

Also one can subclass UIActionSheet in the same way.

这篇关于在iOS 7,SDK 7中关闭UIActionSheet后,键盘会立即隐藏并再次显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 06:36