我有一个有2个场景的故事板。每个场景都有一个segue ctrl +从ViewController图标拖动到另一个场景。从第一个视图到第二个视图具有标识符“left”,从第二个视图到第一个视图 - “right”,两个Segues都指向从UIStoryboardSegue继承的相同自定义类。每个ViewControllers在Attribute Inspector中都有一个标题,并且还没有为它们分配任何自定义类。
在AppDelegate中,我为所有4个方向设置了UISwipeGestureRecognizer,并根据用户滑动的方式,如果当前视图控制器具有标识符为“left”,“right”,“up”或“down”的segue,则会触发performSegueWithIdentifier:< / p>
- (void) handleSwipe:(UISwipeGestureRecognizer *) recognizer {
NSString *direction;
switch ([recognizer direction]) {
case UISwipeGestureRecognizerDirectionLeft:
direction = @"left";
break;
case UISwipeGestureRecognizerDirectionUp:
direction = @"up";
break;
case UISwipeGestureRecognizerDirectionDown:
direction = @"down";
break;
default:
direction = @"right";
break;
}
@try {
UIViewController *rootVC = self.window.rootViewController;
[rootVC performSegueWithIdentifier:direction sender:rootVC];
} @catch (NSException *e) {
NSLog(@"Segue with identifier <%@> does not exist", direction);
}
}
在我的自定义Segue类中,我重写了“perform”方法,如下所示(没什么特别的,因为它按原样打破,但我自然会覆盖它以便以后可以为segue设置自定义动画):
-(void) perform {
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
NSLog(@"source: %@, destination: %@", src.title, dst.title);
[src presentModalViewController:dst animated:NO];
}
然而,它只能在第一次向左滑动时工作一次,之后没有任何反应。我可以通过NSLog中的“执行”方法看到segue的源视图控制器和目标视图控制器在第一次转换后由于某种原因没有改变,只是保持不变。看起来我错过了一些简单的东西,但我无法弄明白。
对我不要太苛刻;),我是iOS开发的新手。
答案 0 :(得分:0)
我认为这是因为你总是在rootViewController上执行segue而segue只是调用presentModalViewController
。一个控制器一次只能有1个模态;如果你想继续呈现模态,你需要从堆栈顶部的视图控制器中呈现它们。我不知道这是不是你的意思。除非你想要能够通过控制器堆向后弹出,否则继续显示模态并没有多大意义。
如果你实际上不想要模态,你可以用目的地替换你的segue中的rootViewController:
-(void) perform {
UIViewController *dst = (UIViewController *) self.destinationViewController;
// do some animation first
[[[UIApplication sharedApplication] delegate].window.rootViewController = dst;
}
还应该注意的是,如你在问题中提到的那样在app委托上设置手势识别器是非常奇怪的。实现自己的UIViewController子类来执行滑动处理并在其自身上调用performSegueWithIdentifier:
会更有意义。