首页 文章

以编程方式立即解除UINavigation视图和模态视图

提问于
浏览
6

我在这个网站上尝试了几个答案,但似乎没有一个与我的问题有关

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

我想要做的是当用户选择一行时,UIAlertView将显示一条消息,同时(不一定是在 same 时间)它解除了UIViewcontroller(模态)和 goes back 推进的Viewcontroller . 基本上,我需要能够解雇所有的viewcontrollers,一个模态和一个push(nav),以便视图返回到他们开始的原始主屏幕 .

我有UIAlertView工作正常,我可以通过使用 [self.dismissViewControllerAnimated:YES completion:nil]; 得到模态viewcontroller,但我不知道如何解雇下一个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

}

3 回答

  • 1

    您可以尝试替换 [self.dismissViewControllerAnimated:YES completion:nil];

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

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

  • 14

    如果需要,可以在iOS 6.0(及更高版本)项目中使用展开segue . 例如,您可以:

    • 在你的顶级视图控制器(你要放松的那个,而不是你要放松的控制器)中,编写一个展开segue方法,在这种情况下称为 unwindToTopLevel (就个人而言,我发现如果segue承担它很有用关于segue的目的地是什么的一些指示,原因在我们到达下面的步骤3时将变得明显:):
    - (IBAction)unwindToTopLevel:(UIStoryboardSegue *)segue
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
    • 在您将从中启动展开的Interface Builder场景中,控制⌘-从视图控制器图标拖动到退出图标以创建展开segue:

    enter image description here

    通常,您将segue从按钮定义到出口(并且已完成),但如果要以编程方式调用segue,则可能需要在控制器和展开出口之间创建它,如上所示 .

    • 你会得到一个小小的弹出窗口,其中包括你展开的segues的名字(来自呈现的控制器...它就像魔术一样):

    enter image description here

    • 如果您要以编程方式调用segue,则必须在IB窗口左侧的文档大纲中选择展开segue,一旦完成,您可以为展开segue提供一个storyboard id:

    enter image description here

    我通常使用展开segue的名称作为故事板id,但你可以使用你想要的任何东西 .

    • 现在,完成后,您的警报视图可以调用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];
        }
    }
    
  • 0

    从课堂上您可以看到您的模态视图

    调用解除模态,然后在一段时间后执行选择器,然后执行

    这是示例代码

    //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 //这里是我想解雇所有VC的地方

    [self.parentViewController dismissAndGoToRoot];
    

    如果这不起作用,则在模态类中获取ParentViewController的实例变量,并在呈现模态视图控制器时分配 .

相关问题