3

My app is crashing as soon as I want to show an alert. This code is pretty basic and I cannot find anything wrong with it. Can someone verify if I am doing something wrong?

@implementation SampleClass
- (void) showAlert
{
UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Make an informed choice"
                      message:@"Descriptive text"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"OK", nil];
[alert show];
[alert release];    
}
@end

The stack is:

0   libobjc.A.dylib                 0x3006bc98 objc_msgSend + 16
1   NESampleApp                     0x002ec5bc 0x1000 + 3061180
2   UIKit                           0x35584bee -[UIWindow _sendTouchesForEvent:] + 362
3   UIKit                           0x35584568 -[UIWindow sendEvent:] + 256
4   UIKit                           0x3556d30c -[UIApplication sendEvent:] + 292
5   UIKit                           0x3556cc4c _UIApplicationHandleEvent + 5084
6   GraphicsServices                0x35350e70 PurpleEventCallback + 660
7   CoreFoundation                  0x3599da90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
8   CoreFoundation                  0x3599f838 __CFRunLoopDoSource1 + 160
9   CoreFoundation                  0x359a0606 __CFRunLoopRun + 514
10  CoreFoundation                  0x35930ebc CFRunLoopRunSpecific + 224
11  CoreFoundation                  0x35930dc4 CFRunLoopRunInMode + 52
12  GraphicsServices                0x35350418 GSEventRunModal + 108
13  GraphicsServices                0x353504c4 GSEventRun + 56
14  UIKit                           0x35597d62 -[UIApplication _run] + 398
15  UIKit                           0x35595800 UIApplicationMain + 664
16  NESampleApp                     0x0017d10c 0x1000 + 1556748
17  NESampleApp                     0x007d3720 0x1000 + 8202016

Update: After setting the delegate to nil, I get some more information along with the crash:

<Error>: -[CALayer isTransformGestureInput]: unrecognized selector sent to instance 0xb4c8a0
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer isTransformGestureInput]: unrecognized selector sent to instance 0xb4c8a0'
341008
  • 9,862
  • 11
  • 52
  • 84

4 Answers4

6

try this instead of [alert show]

[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
John Palmer
  • 25,356
  • 3
  • 48
  • 67
Jordan
  • 331
  • 4
  • 3
2

While this answer may not fix this particular (rather old) question, I did come here because I was having the same symptoms. The answer to my problem was that I was not passing in "nil" as a second item on otherButtonTitles:@"OK".

Bad:

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Make an informed choice"
                      message:@"Descriptive text"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"OK"];

Good:

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Make an informed choice"
                      message:@"Descriptive text"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"OK", nil];

What was interesting is that this only crashed my device, not the iOS simulator.

Jamon Holmgren
  • 23,738
  • 6
  • 59
  • 75
0

Your otherButtonTitles argument needs to be nil-terminated.

In general, methods that take a variable number of arguments, need to have nil at the end. For example:

[NSArray arrayWithObjects:objA, objB, nil];

and in your case:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"otherbutton", nil];
Jerry Thomsan
  • 1,409
  • 11
  • 9
0

Could it be that you are calling this method inside a block? That will cause it to crash. Try this from within your block:

[self performSelectorOnMainThread:@selector(showAlert) 
withObject:nil waitUntilDone:YES];

Had the same problem. This solved it. Answer adopted from: How do display a UIAlertView from a block on iOS?

Community
  • 1
  • 1
JustAnotherCoder
  • 2,565
  • 17
  • 38