iOS:如果应用程序来自后台,则从顶视图控制器重新初始化该应用程序

时间:2016-05-31 07:55:18

标签: ios objective-c iphone uiviewcontroller storyboard

我想从顶视图控制器重新初始化我的应用程序。我希望我的应用程序从头开始重新加载视图。 我在applicationWillEnterForeground方法中试过这个:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
        ActivityController *destViewController = (ActivityController *)[storyboard instantiateViewControllerWithIdentifier:@"ActivityView"];
        [self.navigationController pushViewController:destViewController animated:YES];

但它没有用。

2 个答案:

答案 0 :(得分:0)

如果您不希望自己的应用在后台运行,可以在应用的UIApplicationExitsOnSuspend

中将true设置为Info.plist

答案 1 :(得分:0)

applicationDidEnterBackground和applicationWillEnterForeground,

PrintWriter out = new PrintWriter("filename.txt", "UTF-8");
json = json.replaceFirst("^\uFEFF", "");

并在你的rootViewController的viewDidLoad(它总是出现在应用程序启动时)添加:

  [[NSNotificationCenter defaultCenter] postNotificationName:@"popToRoot" object:nil];

然后在rootViewController中创建一个方法:

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToRootViewControllerAnimated) name:@"popToRoot" object:nil];

您应该使用- (void)popToRootViewControllerAnimated { [self.navigationController popToRootViewControllerAnimated:YES]; } 代替[self.navigationController popToViewController:ActivityController animated:NO];

有关详细信息,请参阅this answer