简体   繁体   English

显示键盘时iOS App崩溃

[英]iOS App crash when displaying Keyboard

We have the following problem and this is how I reproduce it: 我们有以下问题,这是我如何重现它:

  1. I have a textfield in the rootviewcontroller and in viewDidLoad I do: 我在rootviewcontroller和viewDidLoad中有一个文本字段:

     UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; tf.backgroundColor = [UIColor whiteColor]; tf.delegate = self; [self.view addSubview:tf]; [tf becomeFirstResponder]; 
  2. The RootViewController has 2 containers, one for the side menu and one for the actual content-view. RootViewController有2个容器,一个用于侧面菜单,一个用于实际的内容视图。 it DOES delegate UITextFieldDelegate and UITextViewDelegate 它确实委托UITextFieldDelegate和UITextViewDelegate

  3. If we start the app the textfield behaves properly 如果启动应用程序,则文本字段的行为正常
  4. After navigating through several view controllers like this: 在像这样浏览几个视图控制器之后:

     [destination.view setFrame:[[UIScreen mainScreen] applicationFrame]]; [self.currentVC willMoveToParentViewController:nil]; [self addChildViewController:destination]; [self transitionFromViewController:self.currentVC toViewController:destination duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished){ [self.currentVC removeFromParentViewController]; [destination didMoveToParentViewController:self]; self.currentVC = destination; self.currentSelection = selection; }]; 

whenever I want to display the Keyboard using “Cmd+K” (Simulator) I get an EXC_BAD_ACCESS 每当我想使用“ Cmd + K”(仿真器)显示键盘时,我都会得到EXC_BAD_ACCESS

Again the ONLY way to get a crash is to display the system Keyboard 同样,崩溃的唯一方法是显示系统键盘

  1. Enabling Zombies makes it a little bit better but it ends up crashing (ONLY if I open the keyboard). 启用“僵尸”会使它更好一些,但最终会崩溃(仅当我打开键盘时)。
  2. I tried Malloc Guard but the VM runs out of memory and it doesn't really help 我尝试了Malloc Guard,但VM内存不足,并没有真正的帮助

I'm out of ideas! 我没主意了! Here is the trace: 这是跟踪:

0   0x00c1d160 in CALayerGetSuperlayer ()
1   0x00fae832 in -[UIView(Hierarchy) superview] ()
2   0x00fa144a in -[UIView nextResponder] ()
3   0x010f39f1 in -[UIResponder(Internal) _responderWindow] ()
4   0x00fbbc89 in -[UIView(Internal) _firstResponder] ()
5   0x010f25c2 in -[UIResponder isFirstResponder] ()
6   0x01748243 in -[UITextView _keyboardDidShow:] ()
7   0x0222d079 in __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke ()
8   0x02dd8be4 in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
9   0x02cc511b in _CFXNotificationPost ()
10  0x0221c5d6 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
11  0x016fe4e5 in -[UIInputWindowController postEndNotifications:withInfo:] ()
12  0x01705660 in -[UIInputWindowController keyboardHeightChangeDone] ()
13  0x0117da40 in -[UIKeyboardImpl _resizeForKeyplaneSize:splitWidthsChanged:] ()
14  0x012ebf42 in __66-[UIKeyboardPredictionView setPredictionViewState:animate:notify:]_block_invoke ()
15  0x00fb7c06 in +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] ()
16  0x00fb7fc7 in +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] ()
17  0x012ebd31 in -[UIKeyboardPredictionView setPredictionViewState:animate:notify:] ()
18  0x012eb78a in -[UIKeyboardPredictionView setPredictionViewState:animate:] ()
19  0x01193b1a in -[UIKeyboardImpl showKeyboard] ()
20  0x01195fb0 in -[UIKeyboardImpl toggleSoftwareKeyboard] ()
21  0x01196020 in -[UIKeyboardImpl ejectKeyDown] ()
22  0x00f32920 in -[UIApplication _physicalButtonsBegan:withEvent:] ()
23  0x0269a7cd in -[NSObject performSelector:withObject:withObject:] ()
24  0x010f1c94 in forwardTouchMethod ()
25  0x010f4226 in -[UIResponder(Internal) _physicalButtonsBegan:withEvent:] ()
26  0x0269a7cd in -[NSObject performSelector:withObject:withObject:] ()
27  0x010f1c94 in forwardTouchMethod ()
28  0x010f4226 in -[UIResponder(Internal) _physicalButtonsBegan:withEvent:] ()
29  0x0269a7cd in -[NSObject performSelector:withObject:withObject:] ()
30  0x010f1c94 in forwardTouchMethod ()
31  0x010f4226 in -[UIResponder(Internal) _physicalButtonsBegan:withEvent:] ()
32  0x0269a7cd in -[NSObject performSelector:withObject:withObject:] ()
33  0x010f1c94 in forwardTouchMethod ()
34  0x010f4226 in -[UIResponder(Internal) _physicalButtonsBegan:withEvent:] ()
35  0x0176f4d3 in -[UITextField _physicalButtonsBegan:withEvent:] ()
36  0x00f8732a in -[UIWindow _sendButtonsForEvent:] ()
37  0x00f878d8 in -[UIWindow sendEvent:] ()
38  0x00f45681 in -[UIApplication sendEvent:] ()
39  0x00f55ab8 in _UIApplicationHandleEventFromQueueEvent ()
40  0x00f292e7 in _UIApplicationHandleEventQueue ()
41  0x02d3006f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
42  0x02d25b7d in __CFRunLoopDoSources0 ()
43  0x02d250d8 in __CFRunLoopRun ()
44  0x02d24a5b in CFRunLoopRunSpecific ()
45  0x02d2488b in CFRunLoopRunInMode ()
46  0x058352c9 in GSEventRunModal ()
47  0x05835106 in GSEventRun ()
48  0x00f2d0b6 in UIApplicationMain ()
49  0x001cd8ba in main at main.m:14
50  0x03b4cac9 in start ()

Update: I contacted apple support to get help since I HAVE to get this one solved. 更新:由于必须解决此问题,因此我联系了Apple支持以寻求帮助。 My thought is that we are doing some bad memory management but the Profiler shows no Zombies. 我的想法是我们正在执行一些错误的内存管理,但事件探查器未显示任何僵尸。 We have one memory leak though in ADBEncondingStringToHex but were unable to do anything with that info. 虽然在ADBEncondingStringToHex中有一个内存泄漏,但无法对该信息执行任何操作。 Maybe completely unrelated, maybe not... 也许完全无关,也许不...

I think calling [tf becomeFirstResponder] in viewDidLoad is too early - before the view is part of the app's view hierarchy. 我认为在viewDidLoad调用[tf becomeFirstResponder]还为时过早-在视图成为应用程序视图层次结构的一部分之前。 You should call it in viewWillAppear: or viewDidAppear: instead. 您应该在viewWillAppear:viewDidAppear:调用它。

From the docs: 从文档:

You may call this method to make a responder object such as a view the first responder. 您可以调用此方法使一个响应者对象(例如视图)成为第一响应者。 However, you should only call it on that view if it is part of a view hierarchy. 但是,仅当它是视图层次结构的一部分时,才应在该视图上调用它。 If the view's window property holds a UIWindow object, it has been installed in a view hierarchy; 如果视图的window属性包含一个UIWindow对象,则它已安装在视图层次结构中; if it returns nil, the view is detached from any hierarchy. 如果返回nil,则将视图与任何层次结构分离。

This kind of issue happened to me before in iOS8.xx (no problem with iOS7.xx though). 这种问题是我之前在iOS8.xx中发生的(尽管iOS7.xx没有问题)。 I resolved it by calling resignFirstResponder of the textField that becomeFirstResponder before keyboard pop out. 我通过调用becomeFirstResponder键盘之前跳出文本框的resignFirstResponder解决它。 So I think you should call below code to resignFirstResponder of your tf textField before anything that bring out keyboard: 因此,我认为您应该在产生键盘的任何内容之前调用以下代码,将tf textField的resignFirstResponder签名

 [tf resignFirstResponder];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM