質問

I am a newbie in objective c;

I am getting an error at creating a UIAlertview instantiation statements (UIAlert * myAlert, they are in separate scopes)

I have referred the following stackoverflow entries such as This, this, this too and much more research over the internet.

I am unable to get a break through

Following are my alert calls

This is my view controller code where I have put up the "UIAlertViewDelegate"

#import <UIKit/UIKit.h>

@interface gameFirstViewController : UIViewController<UIAlertViewDelegate>

@end

This is my class declaration

#import <Foundation/Foundation.h>

@interface GameLogic : UIView
//all sorts of various non relevant property declarations
@end

Here is my implementation for the alerts

//action to take when an alert is shown
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger) buttonIndex
{  NSLog(@"OK Tapped");
   NSUserDefaults *MySettingsData = [NSUserDefaults standardUserDefaults];
   row=  [MySettingsData integerForKey:@"Row_count"];
   col = [MySettingsData integerForKey:@"Column_count"];
   if(buttonIndex==0)
   {      
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<col;++j)
        {
            myarr[i][j]=0;
        }
    }
    if(_TimerStatus ==1)
        {
            [mainTimer invalidate];
            mainTimer=nil;
            _TimerStatus=0;
        }
        [self super_reset];
    [self setNeedsDisplay];
    NSLog(@"Game reset");
    return;
    }
}

//my usage of the alerts at 2 different places

UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: @"GAME OVER"
                                                         message:@"You clicked on a mine, tap on ok to reset"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles:nil, nil];
        [myAlert performSelectorOnMainThread:@selector(show)
                                  withObject:nil
                               waitUntilDone:YES];

        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: @"You Won! Whoo Hoo"
                                                         message:@"You have successfully dodged every minefield"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles:nil, nil];
                    [myAlert performSelectorOnMainThread:@selector(show)
                                              withObject:nil
                                           waitUntilDone:YES];

I am not sure where I am going wrong, any help would be great!

Thanks.

役に立ちましたか?

解決

The way you are creating the UIAlertView is not safe in ARC. It's fine if you use the object right away, but you're passing it the performSelectorOnMainThread method. ARC may dealloc your myAlert variable by the time the main thread gets to perform the selector. That's likely why you're seeing the EXC_Bad_Request.

Either make myAlert a strong property, so it survives, or delay the creation of the UIAlert until the main thread is able to call the show method. You can do that like this:

dispatch_async(dispatch_get_main_queue(), ^{
    [[[UIAlertView alloc] initWithTitle:@"GAME OVER" message:@"You clicked on a mine, tap on ok to reset" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
});

Note: I removed your double nil for the titles. I don't think that would make a difference, but just in case.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top