popToRootViewControllerAnimated 后程序崩溃

发布于 2024-12-04 05:57:56 字数 2027 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2024-12-11 05:57:56

为什么你要把recipeVC推到分配firstVC的地方。

FirstViewController *firstVC = [[FirstViewController alloc] initWithPageNum:1];

[[ProjectAppDelegate getInstance].mainVC.navigationController 
pushViewController:recipeVC animated:TRUE];

Why are you pushing recipeVC where you are allocating firstVC.

FirstViewController *firstVC = [[FirstViewController alloc] initWithPageNum:1];

[[ProjectAppDelegate getInstance].mainVC.navigationController 
pushViewController:recipeVC animated:TRUE];
黑色毁心梦 2024-12-11 05:57:56

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.

微暖i 2024-12-11 05:57:56

您为什么要将您的应用程序代表类命名为ProjectAppdelegate.h?删除“ .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*)getInstance;
@end

另一个良好的编码实践是手动释放第一个VC,而不是进行自动释放。这种方法要好得多。

- (IBAction)actonFirst:(id)sender 
{
    FirstViewController *firstVC = [[FirstViewController alloc] initWithPageNum:1];
    [[ProjectAppDelegate getInstance].mainVC.navigationController pushViewController:firstVC animated:TRUE];
    [firstVc release];
}

Why are you naming your app delegate class as ProjectAppDelegate.h? Remove the ".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*)getInstance;
@end

Another good coding practice is to release the firstVc manually rather than going for auto release. This approach is much better.

- (IBAction)actonFirst:(id)sender 
{
    FirstViewController *firstVC = [[FirstViewController alloc] initWithPageNum:1];
    [[ProjectAppDelegate getInstance].mainVC.navigationController pushViewController:firstVC animated:TRUE];
    [firstVc release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文