HispaJavi
HispaJavi

Reputation: 13

UIAlertView Crash with no error - Iphone SDK

I have a problem using UIAlertView, if I use it in ViewController class everything is okay, but if I try to use it in external class where I have the general functions (alert for example), on press a button in the alert the application crashes.

    NSString *msg = @"Message";

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert title" message:msg delegate:self cancelButtonTitle:@"Don't remember" otherButtonTitles:@"OK" , @"remember later", nil];
            [alert show];
            [alert release];

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    //never enter here
...

}

When we use this code in the external class, it crashes, don't enter into button click listener.

What is happening?

EDIT: In viewDidLoad I call the external class doing this:

General *generalClass = [[General alloc] init];
[generalClass launchAlert];
[generalClass release];

The external class:

-(void)launchAlert{
      NSString *msg = @"Message";

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert title" message:msg delegate:self cancelButtonTitle:@"Don't remember" otherButtonTitles:@"OK" , @"remember later", nil];
      [alert show];
      [alert release];

      }

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    //never enter here
...

}

Upvotes: 1

Views: 3590

Answers (5)

turtle
turtle

Reputation: 948

You may try it adding a selector to main thread rather than calling it via simple method.

[self performSelectorOnMainThread:@selector(launchAlert) withObject:nil waitUntilDone:NO];

Let me know if it works!

Upvotes: 1

user1250080
user1250080

Reputation: 89

I got same error in iOS 6.

You should implement all protocol methods of UIAlertViewDelegate.

Like willDismissWithButtonIndex, didDismissWithButtonIndex and so on.

I've solved like it. It works fine now.

example:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
}

- (void)alertViewCancel:(UIAlertView *)alertView
{
}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
  return YES;
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  switch(buttonIndex)
  {
     ...
  }
}

Upvotes: 0

ingh.am
ingh.am

Reputation: 26752

Your UIAlert code is fine. Have you tried making your General class a singleton? I've done this in projects before for the exact same reason. That way you can access your predefined messages from anywhere, although it is a little trickier to get the response into the class you called it from but it is possible!

EDIT: If you do make your class a singleton make sure you don't release it!

Upvotes: 0

Moshe
Moshe

Reputation: 58077

Your code looks fine.

Try using the debugger to pinpoint the location of the crash. You can see the debugger by pressing command+Shift+Y. Also, check the crash logs on your device, which are accessible through the Xcode organizer. (command+option+O)

edit:

You are trying to release your external class while the alert is still visible. Use an NSNotification (in the UIAlertView delegate method) to tell the rest of your app when the alert has been dismissed. Then it will be safe to release your external class.

Upvotes: 1

Nathan Gaskin
Nathan Gaskin

Reputation: 1364

Try adding autorelease to the UIAlertView and remove the manual release call.

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Alert title" message:msg delegate:self cancelButtonTitle:@"Don't remember" otherButtonTitles:@"OK" , @"remember later", nil] autorelease];
[alert show];

Upvotes: 0

Related Questions