如何以编程方式从详细信息视图控制器返回到主视图控制器?

时间:2016-01-18 11:17:12

标签: ios iphone swift swift2 navigationcontroller

我一直无法将数据从详细视图控制器传递到主控制器。感兴趣部分的层次结构是

TabBarController -> Profile View Controller --segue--> Navigation Controller -> Detail View Controller

细节视图控制器包含图像的UICollectionView,每个图像都有一个轻击手势识别器。我要做的是点击其中一个图像,将indexPath.row传递给ProfileViewController并以编程方式返回GO BACK。

当我进入Detail View控制器时,已经有一个Back按钮,因此想法是:如果用户按下“Go Back”按钮,则没有任何反应。如果用户按下图像,后退按钮将由其自身触发,并且配置文件中的图像会更改。

到目前为止,我可以点击图片,传递indexPath.row,但我无法回到之前的View Controller。

我已尝试navigationController?.popViewControllerAnimated(true)navigationController?.popToRootViewControllerAnimated(true) 但是它们都不起作用(我将它们放入手势的tap功能中,并且没有任何关于navigationController的事情,tap功能正常工作)

当我打印以下内容时,我得到1 navigationController.viewControllers.count

我设法完成的唯一工作是dismissViewControllerAnimated然而,它在TabBarController之外转移到之前的另一个视图。

2 个答案:

答案 0 :(得分:2)

更新

我想通了,它与程序无关,正确的答案是使用 navigationController?.popViewControllerAnimated(true)但我的视图层次结构错误

TabBarController -> Navigation Controller -> Profile View Controller --segue-> Detail View Controller

现在工作正常

答案 1 :(得分:0)

请在视图控制器Profile View ControllerDetail View Controller中创建具有NSIndexPath强属性的属性。另外,在prepareForSegue中从名为Profile View Controller的视图控制器传递该对象的引用,如下面给出的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        Detail_View_Controller *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyIndexPath:self.object_indexPath];
    }
}

在Detail_View_Controller中,当您点击后退按钮时,请在同一属性中更新您选择的索引,并创建一个IBAction,该IBAction是从前一个视图控制器传递的。

我参考了here

相关问题