如何在故事板中自定义后退按钮 [英] how to custom back button in storyboard

查看:59
本文介绍了如何在故事板中自定义后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有 3 个 ViewController 名称的应用程序:(ViewController,ViewController2,ViewController3)在 ViewController 中存在一个按钮,用于检查文档文件夹中的文件或下载它.例如,首先检查文件是否在文档文件夹中,请转到 ViewController3 否则转到 ViewController2 并下载.

I create one application that has 3 ViewController with names : (ViewController,ViewController2,ViewController3) in ViewController exist one button that is for checking file in document folder or download it.for example first to check file if file exist in document folder go to ViewController3 else go to ViewController2 and download it.

所以 ViewController2 可供下载并具有 UILable &显示下载状态的 UIProgress.如果下载的页面中不存在文件,请转到 ViewController3.

so ViewController2 is for download and has UILable & UIProgress for show download status.if file dont exist in this page downloaded and go to ViewController3.

所以 ViewController3 是用来展示文件的.(这些页面像我底部的图像一样通过 push segue 连接在一起)

so ViewController3 is for show file. (these pages connect together with push segue like my image in bottom)

当我转到任何页面并单击后退按钮返回上一页时,对吗?我什么时候点击第一页中的按钮并且文件不存在并在第二页下载然后完成下载转到第 3 页.现在我想什么时候点击第 3 页中的后退按钮转到第 1 页没有第 2 页!!!!

when I go to any page and I click back button return pervious page right?? I when to click on button in first page and file dont exist and downloading in second page then finished download go to page 3.Now I want when to click on back button in page 3 go to page 1 no page 2!!!!

我从事故事板工作.

推荐答案

您可以通过拦截 ViewController3 中的后退按钮事件并使用 unwindSegues 来实现此目的.查看 William Jockusch 对此问题的回复,了解如何拦截后退按钮事件.

You can achieve this by intercepting the back button event in ViewController3 and using unwindSegues. Check out William Jockusch's reply to this question for how to intercept the back button event.

要在这种特殊情况下使用 unwind segues,您需要:

To use the unwind segues in this particular case you then need to:

1) 在您的 ViewController1 中创建一个方法,例如

1) In your ViewController1 create a method such as

- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
    NSLog(@"Rolled back");
}

2) 在你的故事板中缩小,然后从你的 ViewController3 按住 ctrl-drag 到你的 ViewController3 场景左侧的Exit"绿色框(以及红色框 First Responder 和所有控制器视图的子视图).将显示一个弹出窗口,询问您要将 unwind segue 连接到哪个 IBAction,您应该能够选择您刚刚创建的 unwindToThisViewController 操作.这将创建一个放松转场.

2) In your storyboard zoom out, then ctrl-drag from your ViewController3 to the "Exit" green box on the left contained in your ViewController3 scene (along with the red box First Responder and all your controller view's subviews). A popup window will show, asking what IBAction you want to connect the unwind segue to, and you should be able to select the unwindToThisViewController action that you just created. This will create an unwind segue.

3) 从场景框中选择这个 unwind segue 并给它一个 ID,例如unwindToStart"

3) Select from the scene box this unwind segue and give it an ID, such as "unwindToStart"

4) 最后,在您的 ViewController3 类中,重写方法 viewWillDisappear 如下:

4) Finally, in your ViewController3 class, override the method viewWillDisappear as follows:

-(void) viewWillDisappear:(BOOL)animated 
{
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
        [self performSegueWithIdentifier:@"unwindToStart" sender:self];
    [super viewWillDisappear:animated];
}

这将拦截后退按钮事件并展开到您的 ViewController1,您就可以开始了.

This will intercept the back button event and unroll to your ViewController1, and you're good to go.

由于仅在 iOS 6 及更高版本上支持展开转场,如果您需要在早期版本的 iOS 上使用此功能,我认为唯一的方法是从 ViewController3 的 viewDidLoad 中的 NavigationController 堆栈中手动删除 ViewController2.应该像下面的代码那样做:

As unwind segues are supported only on iOS 6 and later, if you need this functionality on earlier versions of iOS I think the only way is to manually remove the ViewController2 from the NavigationController stack in your ViewController3's viewDidLoad. Something like the following code should do:

- (void)viewDidLoad
{   
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    for(UIViewController* vc in self.navigationController.viewControllers)
    {
        if ([vc isKindOfClass:[ViewController2 class]]) {
            [viewControllers removeObject:vc];
            break;
        }
    }
    self.navigationController.viewControllers = [NSArray arrayWithArray:viewControllers];
    // Do any additional setup after loading the view.
}

这篇关于如何在故事板中自定义后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆