Search code examples
cocoa-touchuitextviewuimenucontroller

Crash upon text selection and deletion from menu in UITextView


My UITextViews crash the app upon text selection and deletion.

enter image description here

Cut, copy, and paste seem to work fine. The only error in the log is:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIMenuController view]: unrecognized selector sent to instance 0xff85ac0'

It is a normal UITextView, and the UIMenuController is untouched and uncustomized. Any ideas?

Happens with iOS 4.3 and 5.0 in simulator and on device.

No more information with zombies enabled, except for a stack trace that I'll need to interpret.. The new Xcode isn't nice about this:

(0x356338bf 0x360ae1e5 0x35636acb 0x35635945 0x35590680 0x37c63925 0x3816b 0x1a557 0x3559222b 0x37a869a7 0x3559222b 0x31671943 0x35607a63 0x356076c9 0x3560629f 0x355894dd 0x355893a5 0x32073fed 0x3794d743 0x29d1 0x2990)

Solution

  • The cause was undocumented (correct me if you find documentation and I'll update this answer) behavior from UITextView and UITextField with their default use of UIMenuController. The "Delete" option is added if your UIText*Delegate implements delete:. When "Delete" is selected from the menu, your field's delegate will then be called with a delete: message. I discovered this by adding an exception breakpoint in the breakpoint listing pane, which gave me the properly symbolicated stack trace that the original exception did not. The crash was caused because I was using delete:(id)sender to service bar button items in a way that required use of the sender. I fixed it by renaming delete: to deletePart:. The user can still delete text by selecting "Cut".