键盘失去隐藏能力“如果我使用UIAlertView” [英] Keyboard loses hiding ability "if I use a UIAlertView"

查看:99
本文介绍了键盘失去隐藏能力“如果我使用UIAlertView”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些iPad应用程序,并且已经开始出现奇怪的效果。

I'm coding some iPad application, and a strange effect has started to appear.

我使用导航栏,以便我可以在我的视图中自由导航。无论如何,在其中一个内部视图中,我提供了检查用户是否真的想通过警报视图返回主视图的可能性(Just a Yes / no按钮)。
一旦用户确认他想要返回,然后我手动弹出视图,然后我导航到键盘隐藏的主视图。

I use a navigation bar so I can navigate freely through my views. Anyway, in one of these innerviews, I offer the possibility of checking if the user really wants to go back to the main view via an alert view (Just a Yes/no pair of buttons). Once the user confirms he wants to go back, then I "manually" pop the view, and I navigate to the main view, where the keyboard hides.

但是,虚拟键盘的行为很怪异。在内部视图内部,键盘在90%的时间内都可见(这是正常的,周围有许多UITextField)。每当我希望隐藏它时,我该怎么做才能隐藏它?我把这样的东西放在main viewWillAppear中:

BUT, the thing is virtual keyboard acts weirdly. Inside the "inner view", the keyboard is visible 90% of the time (It's normal, there are many UITextFields around). What do I do to hide it whenever I want it to become hidden? I put something like this in the "main viewWillAppear":

[self.view endEditing:YES];

显然它隐藏了键盘,当我这样做时:

Apparently it hides the keyboard, and when I do:

[[self navigationController] popToRootViewControllerAnimated:YES]; 

iPad导航回来,然后键盘消失。没什么大不了的(第一次)。

The iPad navigates back, and then the keyboard disappears. No big deal (the first time).

然后我决定重新进入相​​同的视图,所以键盘出现了,我做了一些奇特的东西。然后我决定中止(用我的按钮),我再次显示相同的警报视图,我确认我想回到主视图(就像之前一样)。代码是相同的,但在主视图中,:

But then I decide to reenter the same view, so the keyboard appears, I do some fancy stuff. Then I decide to abort (with my button), I show the same alert view again, and I confirm I want to go back to the main view (just as before). The code is the same, but then, in the main view, the:

[self.view endEditing:YES];

行,什么都不做,最糟糕的是,我已经失去了关闭键盘的能力代码ANYWHERE在我的应用程序中,用于整个会话。

line, does nothing, and what is worst, I've lost the ability to close the keyboard by code ANYWHERE inside my application, for the whole session.

为什么我知道UIAlertView有事可做?因为如果我更改了我的中止按钮中的代码:

Why do I know the UIAlertView has something to do? Because if I change the code inside my "Abort button" from this:

alertViewQuestionType=ALERT_ABANDON_TEST;
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedStringFromTable (@"STR_ABANDON_TEST_WARNING", @"ui_strings", @"") delegate:self cancelButtonTitle:NSLocalizedStringFromTable (@"STR_CANCEL", @"ui_strings", @"") otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:NSLocalizedStringFromTable (@"STR_ABANDON", @"ui_strings", @"")];
[alert show];

到此:

[[self navigationController] popToRootViewControllerAnimated:YES];

(也就是说,无论用户是否确认,用户只需导航回来,只要中止按钮是按下),然后当主视图到达viewWillAppear函数时,隐藏键盘代码,正常工作。不仅是时间,而是所有时间,直到我的应用程序会话结束。

(that is, no confirmation for the user, the user just navigates back, whenever the abort button is pressed), then when the main view reaches the "viewWillAppear" function, the hide keyboard code, works. Not only THAT time, but ALL the time until the end of my app session.

我不知道我的问题是否100%明确,但我会如果有人认为他可以为这个错误提供一些启示,我很高兴在这里添加一些额外的代码...

I don't know if my question is 100% clear, but I'll be more than glad to add some extra pieces of code here if someone believes he can shed some light to this mistery...

问候!

推荐答案

我终于解决了我的问题。我更确定我本可以做得更好,但就目前而言,即使我仍然不知道导致上述行为的原因,我感到非常高兴。

I've finally solved my problem. I'm more than sure that I could have done better, but for now, even I still don't know "what causes the aforementioned behaviour", I'm more than happy.

问题是如果我回弹一个视图,在 UIAlertView 委托代码中的FROM,iOS认为这是我不应该做的事情,它的内部键盘管理代码变得失控。这就好像我太快弹出,或者没有让iOS关闭它需要关闭的任何键盘数据结构。

The thing is that if I "popped back" a view, FROM inside the UIAlertView delegate code, iOS thinks it's something I shouldn't do, and its internal keyboard management code "becomes out of control". It's as if I'm popping back "too soon", or without having let iOS close whatever keyboard data structures it needed to close.

所以同事( aleixventa )给了我一个提示:为什么不延迟一点'popback'代码,将其包含在<$ code> NSTimer 结构?。而这正是我所做的。我的'回弹'手动代码现在是这样的:

So a coworker (aleixventa) gave me a hint: "Why don't you delay a bit the 'popback' code, wrapping it inside a "NSTimer structure"?. And that's precisely what I did. My 'pop back' manual code, is now this:

NSTimer* testTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(popViewPorTimer) userInfo:nil repeats: NO];

为我的 popViewPorTimer 选择器设置一个小函数:

Having a small function for my popViewPorTimer selector:

-(void) popViewPorTimer
{
    [[self navigationController] popViewControllerAnimated:YES];
}

现在,无论键盘通过导航弹回被解除多少次,它总是被隐藏起来。是的! !

And now, no matter how many times the keyboard is dismissed via "Navigation pop back", it always becomes hidden. YAY!!

如果有人知道为什么会发生这种情况,那么这将是一个非常受欢迎的信息。

For extra bonus, if someone knows why all this happens, it will be a more than welcome piece of info.

当我自己解决我的问题时我很喜欢:)

I love when I "solve my questions" by myself :)

这篇关于键盘失去隐藏能力“如果我使用UIAlertView”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆