且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-13 08:31:06

案例1 :放松Segue

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

This will work perfect according to your situation:

iOS Unwind Segue

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];
}