5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

_UIScreenEdgePanRecognizer が原因でクラッシュする

Last updated at Posted at 2015-07-03

前置き

あるときから、こんな感じのエラー報告が度々上がるようになっていました。
再現性もほぼ100%で、どうもnavigationControllerのinteractivePopGestureRecognizerが実行されるときに発生しているようでしたが、直し方がわかりませんでした。

stackTrace
Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x216fd19d

0 libobjc.A.dylib	objc_msgSend + 5 hash
1 CoreFoundation	-[__NSSetM member:] + 68
2 CoreFoundation	-[NSSet containsObject:] + 24
3 UIKit	 -[UIGestureRecognizer _shouldBegin] + 832
4 UIKit	 -[UIGestureRecognizer setState:] + 390
5 UIKit	 -[_UIScreenEdgePanRecognizer _incorporateIncrementalSampleAtLocation:timestamp:modifier:interfaceOrientation:] + 134
6 UIKit	 -[_UIScreenEdgePanRecognizer incorporateTouchSampleAtLocation:timestamp:modifier:interfaceOrientation:] + 138
7 UIKit	 -[UIScreenEdgePanGestureRecognizer touchesMoved:withEvent:] + 216
8 UIKit	 -[UIWindow _sendGesturesForEvent:] + 460
9 UIKit	 -[UIWindow sendEvent:] + 524
10 UIKit	 -[UIApplication sendEvent:] + 196
11 UIKit	 _UIApplicationHandleEventFromQueueEvent + 14414
12 UIKit	 _UIApplicationHandleEventQueue + 1352

原因

わかってしまえば単純なことで、interactivePopGestureRecognizer.delegateにviewDidLoadでselfを指定していたことが原因でした。

この機能について解説してあるブログを読んだときに、viewDidLoadで指定していたので何か理由があると思ってそのまま採用してしまった…。
やはりちゃんと考えないといけませんね。

objective-C
self.navigationController.interactivePopGestureRecognizer.delegate = (id <UIGestureRecognizerDelegate>) self;

というわけで、この記述はviewWillAppear:などに記述しましょう。
また、必要ない画面では逆にnilを指定することを忘れずに。

そうしないと、EdgeGestureが実行されたときに最後にdelegateへ指定したViewControllerを参照しようとして、存在しないviewControllerを参照してcrashしてしまいます。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?