当应用程序从最小化恢复时,我想从AppDelegate呈现两个ViewControllers
我使用applicationWillEnterForeground:
函数来呈现ViewControllers。但是,当我使用下面的代码运行应用程序时,它崩溃并显示错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x1566a420>.'
我使用storyBoards来呈现ViewControllers而不是nib文件。
请帮我解决这个问题,因为我出错了。我一直在努力解决这个问题。
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if(ConditionOne)
{
[self.window.rootViewController presentViewController:[self getMyFilesActivityView] animated:YES completion:nil];
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
else if(ConditionTwo)
{
[self.window.rootViewController presentViewController:[self getPasswordActivityView] animated:YES completion:nil];
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
}
-(MyFilesViewController *)getMyFilesActivityView
{
if(self.myFiles== nil)
{
self.myFiles = [[MyFilesViewController alloc]init];
myFiles = [self.storyboard instantiateViewControllerWithIdentifier:@"MyFilesID"];
}
return self.myFiles;
}
-(PasswordEnterViewController *)getPasswordActivityView
{
if(self.passwordEnter== nil)
{
self.passwordEnter = [[PasswordEnterViewController alloc]init];
passwordEnter = [self.storyboard instantiateViewControllerWithIdentifier:@"PasswordEnterID"];
}
return self.passwordEnter;
}
答案 0 :(得分:0)
这里有些问题。
首先,当您运行此代码时:
[self.window.rootViewController presentViewController:[self getMyFilesActivityView] animated:YES completion:nil];
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
首先会显示视图控制器,然后直接将其关闭,这不是你想要的。你想在显示另一个之前解除旧的视图控制器吗?然后你应该这样做:
[self.window.rootViewController dismissViewControllerAnimated:YES completion:^{
[self.window.rootViewController presentViewController:[self getViewController] animated:YES completion:nil];
}];
此外,你的吸气剂还有一些问题:
self.passwordEnter = [[PasswordEnterViewController alloc]init];
passwordEnter = [self.storyboard instantiateViewControllerWithIdentifier:@"PasswordEnterID"];
第一行分配PasswordEnterViewController,第二行执行相同的操作。在这种情况下,第一行将不执行任何操作(如果self.passwordEnter和passwordEnter甚至是相同的变量,则非常不清楚。)
这就是你的setter看起来的样子:
- (PasswordEnterViewController *)passwordEnter
{
if(_passwordEnter == nil)
{
self.passwordEnter = [self.storyboard instantiateViewControllerWithIdentifier:@"PasswordEnterID"];
}
return _passwordEnter;
}
您的passwordEnter属性应如下所示:
@property (nonatomic, strong) UIViewController *passwordEnter;
此外,请删除您可能正在执行的任何@synthesize
来电,因为您似乎正在覆盖自动合成器,这会使您的代码难以理解。
当您的setter和属性如上所示时,您只需使用以下代码即可显示它:
[self.window.rootViewController presentViewController:self.passwordEnter animated:YES completion:nil];