键盘失去隐藏能力“如果我使用 UIAlertView”
我正在编写一些 iPad 应用程序,并且开始出现奇怪的效果。
我使用导航栏,这样我就可以自由地浏览我的视图。无论如何,在这些内部视图之一中,我提供了检查用户是否真的想通过警报视图返回主视图的可能性(只是一对是/否按钮)。 一旦用户确认他想要返回,我就会“手动”弹出视图,然后导航到键盘隐藏的主视图。
但是,问题是虚拟键盘的行为很奇怪。在“内部视图”内,90% 的时间键盘都是可见的(这很正常,周围有很多 UITextField)。每当我想要隐藏它时,我该怎么做才能隐藏它?我在“主 viewWillAppear”中放置了类似的内容:
[self.view endEditing:YES];
显然它隐藏了键盘,当我这样做时:
[[self navigationController] popToRootViewControllerAnimated:YES];
iPad 导航回来,然后键盘消失。没什么大不了的(第一次)。
但后来我决定重新进入相同的视图,因此键盘出现,我做了一些奇特的事情。然后我决定中止(使用按钮),再次显示相同的警报视图,并确认我想返回主视图(就像以前一样)。代码是相同的,但是在主视图中,:
[self.view endEditing:YES];
行什么也不做,最糟糕的是,在整个会话中,我失去了在应用程序内的任何地方通过代码关闭键盘的能力。
为什么我知道 UIAlertView 有事可做?因为如果我将“中止按钮”内的代码从:更改
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”函数,隐藏键盘代码,有效。不仅是那一次,而且一直到我的应用程序会话结束。
我不知道我的问题是否 100% 清楚,但如果有人相信他可以解开这个谜团,我将非常高兴在这里添加一些额外的代码......
问候!
I'm coding some iPad application, and a strange effect has started to appear.
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.
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];
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];
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.
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];
to this:
[[self navigationController] popToRootViewControllerAnimated:YES];
(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.
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...
Greetings!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于解决了我的问题。我非常确定我可以做得更好,但现在,即使我仍然不知道“是什么导致了上述行为”,我也非常高兴。
问题是,如果我从
UIAlertView
委托代码内部“弹出”一个视图,iOS 会认为这是我不应该做的事情,并且其内部键盘管理代码“变得失控”。就好像我“太快”返回了,或者没有让 iOS 关闭它需要关闭的任何键盘数据结构。因此,一位同事(aleixventa)给了我一个提示:“为什么不延迟一下‘popback’代码,将其包装在“
NSTimer
结构”中?这正是我所做的。我的“弹出返回”手动代码现在是这样的:为我提供一个小函数。
popViewPorTimer
选择器:现在,无论通过“导航弹出返回”关闭键盘多少次,它总是会隐藏起来
! 这将是一个非常受欢迎的信息:)
当我自己“解决我的问题”时,
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.
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.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:Having a small function for my
popViewPorTimer
selector: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 :)
好吧,我一定是完全被误导了,因为我已经删除了所有 .m 文件中的
[self.view endEditing:YES]
和/或resignFirstResponder
的所有痕迹,并且我发现了两件事:1.-他们什么也没做。需要时键盘自动消失。
2.- 我最初描述的错误仍然存在。
因此,据我所知,当我只是在实际的 UINavigationBar“后退”按钮(在任何内部视图中)中“按后退”时,视图会弹出,如果键盘存在,它会弹出消失。到目前为止,一切都很好。
然后有时,我需要模拟“后退”按钮。我这样做的是:
它工作完美,也就是说,它只是弹回来,如果键盘存在,它就会消失。
但是,如果我在我也需要的一些
UIAlertView
委托中编写这个“手动后退”操作(我处理两个接受/取消按钮),这个“手动后退操作”也会弹出到“父级” “视图(总是可以的),但它仅在第一次调用时隐藏键盘。通过此接受/取消UIAlertDelegate
函数连续调用“手动返回”,使“键盘隐藏引擎”在会话的其余部分完全无法使用。我简直迷路了。
Well, I must have been completely misled, because I've removed all traces of
[self.view endEditing:YES]
and/orresignFirstResponder
in ALL my .m files and I've discovered 2 things:1.- They weren't doing ANYTHING. Keyboard auto disappears when needed.
2.- The bug I originally described, is still there.
So, as far as I know, when I just "press back" in the actual
UINavigationBar
"Back" button (in any inner view), the view, pops back, and if keyboard was present, it disappears. So far so good.Then some other times, I need to simulate a "Back" press button. I do it with:
It works flawlessly, that is, it just pops back, and if keyboard was present, it disappears.
BUUUT, if I write this "manual back" action INSIDE some
UIAlertView
Delegate I also need (where I deal with two Accept/Cancel buttons), this "manual back action" also pops back to the "parent" view (always, that's OK), but it hides the keyboard ONLY the first time it's called. Succesive calls to "manual back" via this accept/cancelUIAlertDelegate
function, render the "keyboard hiding engine" completely unusable for the rest of the session.I'm plain lost.