如何自定义故事板中的button
我创build了一个具有3个ViewController的应用程序,在ViewController中有(ViewController,ViewController2,ViewController3)存在一个button,用于检查文档文件夹中的文件或下载它。例如,首先检查文件是否存在于文档文件夹中去ViewController3否则去ViewController2并下载它。
所以ViewController2是下载和具有UILable&UIProgress显示下载status.if文件不存在在此页面下载并转到ViewController3。
所以ViewController3是用于显示文件。 (这些页面连接在一起,像我的图像在底部推推)
当我去任何页面,我点击返回button返回前页右? 我什么时候点击第一页上的button,文件不存在,下载第二页,然后完成下载,转到第3页。现在我想点击第3页上的后退button,转到第1页,第2页!
我在故事板上工作。
您可以通过拦截ViewController3中的后退button事件并使用unwindSegues来实现此目的。 查看William Jockusch对这个问题的回答,了解如何拦截后退button事件。
要在这种特殊情况下使用展开式塞格,您需要:
1)在你的ViewController1中创build一个方法如
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue { NSLog(@"Rolled back"); }
2)在你的故事板缩小,然后按住Ctrl从你的ViewController3拖动到你的ViewController3场景中的左边的“退出”绿色框(以及红色框First Responder和所有控制器视图的子视图)。 一个popup窗口将会显示,询问你想要连接unwind segue的IBAction,并且你应该能够select刚才创build的unwindToThisViewController
动作。 这将创造一个放松的继续。
3)从场景框中select这个展开segue,并给它一个ID,如“unwindToStart”
4)最后,在你的ViewController3类中,重写方法viewWillDisappear
,如下所示:
-(void) viewWillDisappear:(BOOL)animated { if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) [self performSegueWithIdentifier:@"unwindToStart" sender:self]; [super viewWillDisappear:animated]; }
这将拦截后退button事件,并展开到您的ViewController1,你很好去。
编辑:因为unwind segues只支持iOS 6和更高版本,如果你需要在早期版本的iOS上的这个function,我想唯一的办法是手动从ViewController3的viewDidLoad的NavigationController堆栈中删除ViewController2。 像下面的代码应该这样做:
- (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. }