我正在尝试使用模态视图控制器为我的应用程序创建1-2秒的闪屏但是当我尝试关闭视图时,我的应用程序崩溃时出现错误的访问错误。所以在我的申请代表中我有:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(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];
}
这一切都运行得很好,除非在2秒后调用hideSplash时应用程序崩溃并使用EXC_BAD_ACCESS。如果我注释掉执行选择器行并立即调用hidesplash:
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
[self hideSplash];
模态视图被正确驳回。我很确定这是一个内存管理问题,但我不确定我在这里做错了什么。有没有人知道这可能是什么或如何正确调试这样我可以推迟解雇?
由于
答案 0 :(得分:1)
这看起来很奇怪:
mainViewCtrl = [MainViewController alloc];
尝试添加初始化调用。
答案 1 :(得分:1)
//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];
将上述内容更改为以下内容:
//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 !!!!