如何在视图控制器中创建后退按钮以转到父视图控制器

如何在视图控制器中创建后退按钮以转到父视图控制器

本文介绍了如何在视图控制器中创建后退按钮以转到父视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有一个带有容器的视图控制器,并且在容器中有一个带有集合视图的子视图,当用户点击集合视图单元时,它将我发送给详细视图控制器,但现在我要做的是添加一个返回我的详细视图控制器中的按钮,它将我发送到parentViewController

hi I have a view controller with a container and in the container a child view with a collection view when the user taps the collection view cell it sends me to detail view controller but now what i want to do is to add a back button in my detail view controller which sends me to the parentViewController

推荐答案

案例1 :放松Segue

根据您的具体情况,这将完美无缺:

This will work perfect according to your situation:

Unwind Segues为您提供了一种展开导航堆栈并指定要返回的目的地的方法。

Unwind Segues give you a way to "unwind" the navigation stack and specify a destination to go back to.

案例2 :PopToRootViewController

Case 2 : PopToRootViewController

如果您的父视图也是您的根视图控制器,然后您可以使用轻松取回popToRootViewControllerAnimated:YES

If you Parent view is also your Root view controller, then you can easily get back using popToRootViewControllerAnimated:YES.

创建自己的后退按钮将其添加到导航栏方法 backButtonTouch

Create own back button add it to navigation bar with method backButtonTouch.

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStyleDone target:self action:@selector(backButtonTouch:)];

将以上代码添加到 viewDidLoad

-(void)backButtonTouch:(id)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

这篇关于如何在视图控制器中创建后退按钮以转到父视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 02:38