Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

segue change destinationViewController

Is there a way to change what controller the segue is going to invoke in prepare for segue? I'm trying to do that when a segmentedcontrol is changed with an embedded segue. Thanks!

like image 572
Cocoa Nub Avatar asked Dec 11 '22 20:12

Cocoa Nub


2 Answers

As you may have noticed the segue's destinationViewController is readonly. Your better strategy would be to have segues defined between the view controller that holds the segmented control (not a view or control) and the other view controllers that you want to choose among. Make your decision based on the selected segment and call performSegueWithIdentifier:sender: from the controller's code with the identifier that matches the segment.

like image 58
Phillip Mills Avatar answered Jan 09 '23 07:01

Phillip Mills


If you want to switch which controller is the embedded controller, then I think you need to use the custom container view controller paradigm that Apple uses. The code I have below is from a small test app that does that. This was set up using the single controller template, and then adding a container view to that controller (called ViewController), and a segmented control to the main view. I then added a disconnected view controller, changed its size to free form, and then adjusted its view size to be the same as the embedded controller's view size. Here is the code in ViewController.h:

@interface ViewController : UIViewController

@property (weak,nonatomic) IBOutlet UIView *container;
@property (strong,nonatomic) UIViewController *initialVC;
@property (strong,nonatomic) UIViewController *substituteVC;
@property (strong,nonatomic) UIViewController *currentVC;

@end

And this is what I have in the ViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.initialVC = self.childViewControllers.lastObject;
    self.substituteVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Substitute"];
    self.currentVC = self.initialVC;
}

-(IBAction)SwitchControllers:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
        case 0:
            if (self.currentVC == self.substituteVC) {
                [self addChildViewController:self.initialVC];
                self.initialVC.view.frame = self.container.bounds;
                [self moveToNewController:self.initialVC];
            }
            break;
        case 1:
            if (self.currentVC == self.initialVC) {
                [self addChildViewController:self.substituteVC];
                self.substituteVC.view.frame = self.container.bounds;
                [self moveToNewController:self.substituteVC];
            }
            break;
        default:
            break;
    }
}


-(void)moveToNewController:(UIViewController *) newController {
    [self.currentVC willMoveToParentViewController:nil];
    [self transitionFromViewController:self.currentVC toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{}
                            completion:^(BOOL finished) {
                                [self.currentVC removeFromParentViewController];
                                [newController didMoveToParentViewController:self];
                                self.currentVC = newController;
                            }];
}
like image 28
rdelmar Avatar answered Jan 09 '23 09:01

rdelmar