ios - 以编程方式同时关闭 UINavigation View 和模态视图

标签 ios objective-c cocoa-touch uinavigationcontroller storyboard

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

我有一个 MasterDetail 应用程序,它有两种我正在使用的转场类型。当您按下详细 View 上的按钮时,它会使用推送转场并将另一个详细 View 推送到该按钮上。在新的详细 View (刚刚按下的那个)中,有一个按钮可以使用模式转场调用另一个 UIView(表单)。

我想做的是当用户选择一行时,UIAlertView 将显示一条消息,同时(不必在相同时间)它解除 UIViewcontroller(模态)并返回从被推送的 Viewcontroller。基本上,我需要能够关闭所有 View Controller ,一个模式和一个推送 (nav),以便 View 返回到它们开始时的原始主屏幕。

我的 UIAlertView 工作正常,我可以通过使用 [self.dismissViewControllerAnimated:YES completion:nil]; 关闭模态视图 Controller ,但我不知道如何关闭下一个 Viewcontroller (在导航 Controller 中)。使用这个:[self.navigationController popToRootViewControllerAnimated:NO]; 不起作用。

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

- (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. 在您的顶级 View Controller (您想要展开的 Controller ,而不是您要展开的 Controller )中,写一个展开segue 方法,在本例中称为 unwindToTopLevel(就我个人而言,如果 segue 带有一些关于 segue 的目的地是什么的指示,我发现它很有用,原因在我们进入第 3 步时会变得明显, 下面):

    - (IBAction)unwindToTopLevel:(UIStoryboardSegue *)segue
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
  2. 在您将启动展开的 Interface Builder 场景中,控制 ⌘ - 从 View Controller 图标拖动到退出图标以创建展开转场:

    enter image description here

    通常,您会定义从按钮到导出导出的转场(这样就完成了),但是如果您想以编程方式调用转场,您可能希望在 Controller 和展开导出之间创建它,例如如上所示。

  3. 您会看到一个小弹出窗口,其中包含您展开的 segues 的名称(来自呈现 Controller ……就像变魔术一样):

    enter image description here

  4. 如果您要以编程方式调用该转场,您必须在 IB 窗口左侧的文档大纲中选择该展开转场,完成后,您可以提供展开转场 Storyboard ID:

    enter image description here

    我通常使用 unwind segue 的名称作为 Storyboard ID,但您可以使用任何您想要的名称。

  5. 现在,完成后,您的警报 View 可以调用 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];
        }
    }
    

关于ios - 以编程方式同时关闭 UINavigation View 和模态视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17606493/

相关文章:

javascript - 通过 Ionic 检测耳机

ios - 在 setRegion 上取消选择 MKAnnotationView

ios - NSDateFormatter "HH"在 iOS 8 设备上返回上午/下午

iphone - 自动化 iPhone 应用程序的特定状态

iphone - 代码签名错误 : code signing is required for product type 'Application' in SDK 'iOS 4.2'

ios - 在 iOS 应用程序中使用 AWS Cognito 和 Facebook 登录时 IdentityId 为 Nil

ios - 自定义单元格中的按钮操作不调用委托(delegate)函数

ios - CoreData NSPredicate 按日期获取数据

ios - 从 Objective-C 访问 Swift 扩展

cocoa-touch - UIModalPresentationFormSheet 白角