I am trying to create a 1-2 second splashcreen for my application using a modal view controller however when i try to dismiss the view my application crashes with a bad access error. So in my application delegate i have:
- (BOOL) application:(UIApplication *)application didFin开发者_运维百科ishLaunchingWithOptions:(NSDictionary *)launchOptions {
//create window and show it
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
window.backgroundColor = [UIColor greenColor];
[window makeKeyAndVisible];
//create the main view controller and add its view to the window
mainViewCtrl = [MainViewController alloc];
[window addSubview:mainViewCtrl.view];
//show splash screen
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"default.png" ofType:nil]];
splashViewCtrl = [[UIViewController alloc] init];
splashViewCtrl.view = [[UIImageView alloc] initWithImage:image];
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
//setup callback to dismiss
[self performSelector:@selector(hideSplash) withObject:nil afterDelay:2.0];
return(true);
}
//hide splash screen callback
- (void)hideSplash {
[[self mainViewCtrl] dismissModalViewControllerAnimated:YES];
}
And this all works perfectly fine except when the hideSplash is called after 2 seconds the application crashes with a EXC_BAD_ACCESS. If i comment out the perform selector line and call the hidesplash immediately after like so:
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
[self hideSplash];
The modal view is properly dismissed. I'm fairly sure this is a memory management problem but i'm not sure exacty what i'm doing wrong here. Does anyone have any ideas of what this could be or how to properly debug this so i can delay the dismissal?
Thanks
This looks weird:
mainViewCtrl = [MainViewController alloc];
Try adding the initialisation call to it.
//show splash screen
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"default.png" ofType:nil]];
splashViewCtrl = [[UIViewController alloc] init];
splashViewCtrl.view = [[UIImageView alloc] initWithImage:image];
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
Change the above to this below:
//show splash screen
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"default.png" ofType:nil]];
splashViewCtrl = [[UIViewController alloc] init];
splashViewCtrl.view = [[UIImageView alloc] initWithImage:image];
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
[mainViewCtrl release]; //Add this line !!!!
精彩评论