开发者

UIAlertView Crash with no error - Iphone SDK

开发者 https://www.devze.com 2023-02-13 00:34 出处:网络
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

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{
  开发者_Go百科    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
...

}


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.


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!


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];


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!


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)
  {
     ...
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消