[英]Custom segue to push a specific UIViewController to UINavigationController
我想创建一个自定义segue,其作用方式与在UINavigationController视图控制器上使用时标准push segue相同。 我已经实现了我的自定义segue:
CustomSegue.m
-(void)perform {
UIViewController *source = (UIViewController *)[self sourceViewController];
UIViewController *dest =(UIViewController *)[self destinationViewController];
if (1==2 //test) {
[source.navigationController pushViewController:dest animated:YES];
}
else {
UIViewController *altDest = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL]
instantiateViewControllerWithIdentifier:@"alternateView"];
[source.navigationController pushViewController:altDest animated:YES];
}
正如您所看到的,我想使用自定义推送segue的原因是我可以根据用户配置决定推送哪个视图控制器(目前只检查一个简单的1 == 2表达式)。 我可以毫无问题地实例化备用视图控制器,但我希望能够做的是每次都不来重新加载视图控制器(使用后退和下一个按钮)。 有没有办法从故事板中检索现有实例,或者这样做的一些标准方法?
而不是自定义segue与其perform
,做你所描述的方式,即实时选择是否推动dest
或altDest
,是(1)根本不使用segues并直接调用pushViewController
,就像你在这里做的一样,或(2)准备从视图控制器整体发出的两个segue,并调用performSegueWithIdentifier:
来说明我们应该执行哪些。
至于直接从dest
到altDest
,你可以在dest
顶部推动altDest
,然后从导航控制器的视图控制器的堆栈中删除dest
。
就像关于iOS一样,如果你根本不使用故事板,这就更容易也更明显了。 这就是为什么我不喜欢故事板:它们是如此简单和限制,它们分散了人们对iOS 真正工作方式的注意力。
没有办法从故事板中检索现有的控制器 - 如果有一个controllerWithIdentifier:方法来做这件事会很好,但事实并非如此。 Segues(除了unwinds)总是实例化新的控制器,所以我认为你不能用segue做你想做的事。 如果你想多次前进(推送)到同一个控制器,那么你需要在代码中通过创建一个指向你的控制器的属性,并在推送它之前检查该控制器是否存在来完成它。
正如其他人所指出的那样,你不能使用segue来推送到现有的控制器实例。 执行segue的过程总是为您创建一个新的实例作为目标控制器。
就个人而言,当我在视图控制器的现有实例之间跳转时,我认为“容器视图控制器”,例如UIPageViewController
,这使得在两个或更多控制器之间转换非常容易,而不必每次都重新实例化它们。
如果您不喜欢页面视图控制器所施加的约束(例如,您可能不喜欢iOS 5版本仅支持页面卷曲过渡,或者iOS 6仅添加滚动过渡,并且您想要其他内容),然后你会做一个自定义容器视图控制器。
例如,如果我想在两个视图控制器之间跳转而不是每次都重新实例化它们,我首先要创建一个自定义容器视图控制器,即“父”,并确保我有一个属性来跟踪哪个孩子我'我目前在:
@property (nonatomic) NSInteger childViewIndex;
如果仅支持iOS 6.0及更高版本,我会在父视图控制器的场景中添加“容器视图”。 如果支持6.0之前的iOS版本,我会在场景中添加一个标准的UIView
,然后手动实例化第一个子控制器:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIViewController *controller;
// add the first child
UIViewController *controller = [self addChildWithIdentifier:@"One"];
[self.containerView addSubview:controller.view];
[controller didMoveToParentViewController:self];
self.childViewIndex = 0;
}
- (UIViewController *)addChildWithIdentifier:(NSString *)storyboardIdentifier
{
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:storyboardIdentifier];
[self addChildViewController:controller];
controller.view.frame = self.containerView.bounds;
controller.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
return controller;
}
然后,当我想转换到第二个子节点(或转换回第一个子节点)时,我将在父视图控制器中调用以下例程:
- (void)transitionToViewControllerIndex:(NSInteger)index
{
// don't do anything if we're trying to transition to ourselves!
if (index == self.childViewIndex)
return;
// identify the two controllers in question
UIViewController *sourceController = self.childViewControllers[self.childViewIndex];
UIViewController *destinationController;
// if we're asking for page 2, but we only have one defined, then we'll have to instantiate it
BOOL instantiateDestination = (index == 1 && [self.childViewControllers count] < 2);
if (instantiateDestination)
destinationController = [self addChildWithIdentifier:@"Two"];
else
destinationController = self.childViewControllers[index];
// configure the destination controller's frame
destinationController.view.frame = sourceController.view.frame;
// if you're jumping back and forth, set the animation appropriate for the
// direction we're going
UIViewAnimationOptions options;
if (index > self.childViewIndex)
{
options = UIViewAnimationOptionTransitionFlipFromRight;
}
else
{
options = UIViewAnimationOptionTransitionFlipFromLeft;
}
// now transition to that destination controller
[self transitionFromViewController:sourceController
toViewController:destinationController
duration:0.5
options:options
animations:^{
// for simple flip, you don't need anything here,
// but docs say this can't be NULL; if you wanted
// to do some other, custom annotation, you'd do it here
}
completion:^(BOOL finished) {
if (instantiateDestination)
[destinationController didMoveToParentViewController:self];
}];
self.childViewIndex = index;
}
因此,要转换到第二个子视图控制器,您只需调用:
[self transitionToViewControllerIndex:1];
如果您想转换回来,可以致电:
[self transitionToViewControllerIndex:0];
我只是在这里表面,但容器视图控制器(或者如果没有标准的那些为您完成工作,自定义容器视图控制器)正是您所需要的。
有关更多信息,请参阅:
在适用于iOS的View Controller编程指南中 创建自定义容器视图控制器 。
在WWDC 2011会话视频中 实现UIViewController包含 (需要Apple开发人员ID)。
在UIViewController类参考中 实现Container View Controller 。
适用于iOS的View Controller Catalog中的页面视图控制器 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.