Force iphone app to restart programmatically?

前端 未结 7 990
北荒
北荒 2020-11-27 03:28

I am trying to get my iPhone app to restart programmatically when the Logout button is pressed.

Has anyone got a code sample to share? I\'ve read that it is possible

7条回答
  •  再見小時候
    2020-11-27 04:07

    try this, it works for me.

    -(void)restart
    {
        MyAppDelegate *appDelegate = (MyAppDelegate *)([UIApplication sharedApplication].delegate);
        [appDelegate.navigationController popToRootViewControllerAnimated:NO];
        UIViewController *topViewController = appDelegate.navigationController.topViewController;
        Class class = [topViewController class];
        NSString *nibName = topViewController.nibName;
        UIViewController *rootViewcontroller = (UIViewController *)([[class alloc] initWithNibName:nibName bundle:nil]);
        [appDelegate.navigationController.view removeFromSuperview];
        appDelegate.navigationController.viewControllers = [NSArray arrayWithObject:rootViewcontroller];
        [appDelegate.window addSubview:appDelegate.navigationController.view];
        [appDelegate.window makeKeyAndVisible];
    }
    

提交回复
热议问题