以编程方式同时消除UINavigation视图和Modal视图

我已经在这个网站上尝试了一些答案,但他们都没有涉及到我的问题

我有一个MasterDetail应用程序,它有两种types的segues,我正在使用。 当您按下详细视图上的button时,它会使用推入区域并将另一个详细视图推送到该视图上。 在新的detailview中(刚刚推出的那个),有一个button使用模态segue调用另一个UIView(表单)。

我想要做的是当用户select一行时,UIAlertView将显示一条消息,而同时(不必在同一时间)它将closuresUIViewcontroller(模态)并返回从推送的ViewController。 基本上,我需要能够closures所有视图控制器,一个模式和一个推(导航),使视图返回到他们开始的原始主屏幕。

我有UIAlertView工作正常,我可以通过使用[self.dismissViewControllerAnimated:YES completion:nil];来取消模态viewcontroller [self.dismissViewControllerAnimated:YES completion:nil]; 但我不知道如何解散下一个ViewController(它在导航控制器中)。 使用这个: [self.navigationController popToRootViewControllerAnimated:NO]; 不起作用。

这里是我想调用函数来删除所有视图的地方:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlWithIDAndChallenge]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; UIAlertView *message = [[UIAlertView alloc] initWithTitle@"Started" message:@"The challenge has begun!" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil]; [message show] //right here is where I would normally dismiss the modal VC //here is where I would like to dismiss all VC } 

如果你想,在iOS 6.0(和更高版本)的项目中,你可以使用unwind segue。 例如,您可以:

  1. 在你的顶级视图控制器(你想放松的那个控制器,而不是你要放松的控制器)中,写一个unwind segue方法,在这种情况下叫做unwindToTopLevel (我个人觉得它有用,如果segue承载一些指示赛格的目的地是什么,因为当我们到达下面的步骤3时将会变得明显):

     - (IBAction)unwindToTopLevel:(UIStoryboardSegue *)segue { NSLog(@"%s", __FUNCTION__); } 
  2. 在您将从中启动展开的Interface Builder场景中, 控制⌘- drag从视图控制器图标到出口图标以创build展开的渐变:

    在这里输入图像说明

    一般来说,你需要从一个button到出口定义segue(并且你已经完成了),但是如果你想以编程的方式调用segue,你可能想在controller和unwind outlet之间创build它,如上所示。

  3. 你会得到一个小小的popup窗口,其中包括你展开顺序的名字(来自于演示控制器…就像魔术一样):

    在这里输入图像说明

  4. 如果要以编程方式调用segue,则必须在IB窗口左侧的文档大纲中select展开segue,一旦完成了这一步,就可以为展开的segue添加一个storyboard ID:

    在这里输入图像说明

    我通常使用故事板ID的unwind segue的名字,但你可以使用任何你想要的。

  5. 现在,这样做,你的警报视图可以调用segue:

     - (IBAction)didTouchUpInsideButton:(id)sender { [[[UIAlertView alloc] initWithTitle:nil message:@"go home" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil] show]; } #pragma mark - UIAlertViewDelegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex != [alertView cancelButtonIndex]) { [self performSegueWithIdentifier:@"unwindToTopLevel" sender:self]; } } 

从class上你介绍了你的模态观点

调用解除模态,然后执行一些延迟后select器,然后做

这里是示例代码

 //Write this in the class from where you presented a modal View. //Call the function from modal class when you want to dismiss and go to root. - (void) dismissAndGoToRoot { [self dismissViewControllerAnimated:YES completion:nil]; [self performSelector:@selector(gotoRoot) withObject:nil afterDelay:0.50]; } - (void)gotoRoot { [self.navigationController popToRootViewControllerAnimated:NO]; } 

在这里您将调用该函数

//在这里,我通常会忽略模态VC //这里是我想要closures所有VC的地方

 [self.parentViewController dismissAndGoToRoot]; 

如果这不起作用,那么在模态类中获取ParentViewController的实例variables,并在呈现模态视图控制器时分配。

你可以尝试replace[self.dismissViewControllerAnimated:YES completion:nil];

 self dismissViewControllerAnimated:YES completion:^{ [parentViewController.navigationController popToRootViewControllerAnimated:YES]; } 

我相信parentViewController将指向呈现视图控制器,该块将导致父视图控制器的导航控制器popup到根视图控制器。