0
votes

I have a storyboard with 2 scenes. Each scene has a segue ctrl+dragged from ViewController icon to the other scene. Segue from first view to the second has identifier "left" and from the second to the first - "right", both Segues point to the same custom class inherited from UIStoryboardSegue. Each ViewControllers has a title in Attribute Inspector, and don't have any custom classes assigned to them just yet.

In AppDelegate I have UISwipeGestureRecognizer setup for all 4 directions and according to how user swipes if the current view controller has a segue with identifier "left", "right", "up", or "down" it fires performSegueWithIdentifier:

- (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);
    }
}

In my custom Segue class I override the "perform" method as follows (nothing fancy, since it breaks as is, but naturally I override it to be able to have a custom animation for segues later):

-(void) perform {
    UIViewController *src = (UIViewController *) self.sourceViewController;
    UIViewController *dst = (UIViewController *) self.destinationViewController;

    NSLog(@"source: %@, destination: %@", src.title, dst.title);

    [src presentModalViewController:dst animated:NO];
}

However, it only works once on the first swipe to the left and after that nothing happens. I can see via the NSLog in the "perform" method that segue's source and destination view controllers don't change for some reason after this first transition and just stay the same onward. It looks like I am missing something simple, but I can't figure it out.

Don't be too harsh on me ;) , I am new to the iOS development.

1

1 Answers

0
votes

I think it's because you're always performing the segue on the rootViewController and the segue is just calling presentModalViewController. A controller can only have 1 modal at a time; if you want to keep presenting modals you need to present them from the view controller at the top of the stack. I have no idea if that's what you mean to do.. it doesn't really make sense to keep showing modals unless you want to be able to pop backwards through the controller stack.

If you don't actually want the modals, you can just replace the rootViewController in your segue with the destination:

-(void) perform {
    UIViewController *dst = (UIViewController *) self.destinationViewController;
    // do some animation first
    [[[UIApplication sharedApplication] delegate].window.rootViewController = dst;
}

It should also be noted that it is very weird to have a gesture recognizer on the app delegate like you mention in your question. It would make a lot more sense to implement your own UIViewController subclass that does the swipe handling and calls performSegueWithIdentifier: on itself.