popToRootViewControllerAnimated 后程序崩溃
我在 MainMenuViewController 类中有一个 navigationController 。当我将 FirstViewController 推入 navigationController 时,我进入第二个场景(FirstViewController),一切正常。但是,当我想返回根控制器(MainMenuViewController)时,我的程序在 main.m 中崩溃,并出现错误线程 1:程序收到信号:“EXC_BAD_ACCESS”。你能帮助我吗?
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); // CRASH
[pool release];
return retVal;
}
ProjectAppDelegate.h:
@interface ProjectAppDelegate: NSObject <UIApplicationDelegate> {
UIWindow *window;
MainMenuViewController *mainVC;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MainMenuViewController *mainVC;
@property (nonatomic, retain) UINavigationController *navigationController;
+(ProjectAppDelegate.h*)getInstance;
@end
ProjectAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ProjectAppDelegateInstance = self;
self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.navigationController setNavigationBarHidden:TRUE];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
MainMenuViewController.m
- (IBAction)actonFirst:(id)sender
{
FirstViewController *firstVC = [[[FirstViewController alloc] initWithPageNum:1] autorelease];
[[ProjectAppDelegate getInstance].mainVC.navigationController pushViewController:firstVC animated:TRUE];
}
FirstViewController.m
- (IBAction)actonHome:(id)sender
{
[[ProjectAppDelegate getInstance].mainVC.navigationController popToRootViewControllerAnimated:TRUE];
}
I have a navigationController in MainMenuViewController class. When I push FirstViewController in navigationController I go to my second scene (FirstViewController) and it's ok. But when I'd want go back to root controller (MainMenuViewController) my program crash in main.m with error Thread 1:Program received signal: "EXC_BAD_ACCESS". Can you help me?
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); // CRASH
[pool release];
return retVal;
}
ProjectAppDelegate.h:
@interface ProjectAppDelegate: NSObject <UIApplicationDelegate> {
UIWindow *window;
MainMenuViewController *mainVC;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MainMenuViewController *mainVC;
@property (nonatomic, retain) UINavigationController *navigationController;
+(ProjectAppDelegate.h*)getInstance;
@end
ProjectAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ProjectAppDelegateInstance = self;
self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.navigationController setNavigationBarHidden:TRUE];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
MainMenuViewController.m
- (IBAction)actonFirst:(id)sender
{
FirstViewController *firstVC = [[[FirstViewController alloc] initWithPageNum:1] autorelease];
[[ProjectAppDelegate getInstance].mainVC.navigationController pushViewController:firstVC animated:TRUE];
}
FirstViewController.m
- (IBAction)actonHome:(id)sender
{
[[ProjectAppDelegate getInstance].mainVC.navigationController popToRootViewControllerAnimated:TRUE];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么你要把recipeVC推到分配firstVC的地方。
Why are you pushing recipeVC where you are allocating firstVC.
actionFirst 方法中的recipeVC 是什么?首先检查一下..
我在我的一个项目中也遇到了这种问题。make firstVC 是 MainMenuViewController 和 MainMenuViewController 的属性。在 dealloc() 中释放它。
尝试一下对我有用的。
What is recipeVC in actionFirst Method ? first check it ..
I was also face this kind of problem in one of my project.make firstVC is the property of MainMenuViewController & release it in dealloc().
Try it that works for me.
您为什么要将您的应用程序代表类命名为ProjectAppdelegate.h?删除“ .h”。
另一个良好的编码实践是手动释放第一个VC,而不是进行自动释放。这种方法要好得多。
Why are you naming your app delegate class as ProjectAppDelegate.h? Remove the ".h".
Another good coding practice is to release the firstVc manually rather than going for auto release. This approach is much better.