开发者

Program crashes after popToRootViewControllerAnimated

开发者 https://www.devze.com 2023-04-04 22:12 出处:网络
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

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) IB开发者_如何学GoOutlet 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];
}


Why are you pushing recipeVC where you are allocating firstVC.

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

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


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.


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号