Perform parent segue from child to child??

Discussion in 'iOS Programming' started by DennisBlah, Feb 9, 2015.

  1. DennisBlah, Feb 9, 2015
    Last edited: Feb 11, 2015

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all,

    I have a small question which might be easy to answer for one of you all.

    I have a 'rootViewController' that has an header, container and footer
    (All with costum backgrounds etc)

    I have several buttons on the rootViewController which trigger a costum segue to load lets say homeViewcontroller or secondViewController into the container.

    Now I what I would like to do is.. From homeViewController I'd like to put a button that will go to previewViewController
    But it must be loaded into the container of rootViewController.
    But the button that will trigger the segue is not in the rootViewController but in the homeViewController.

    Anyone understands what I mean ? And can help out? It would be great.

    All I can get to is go to the previewViewController from the home, but it will lose the 'layout' of the rootViewController.

    This is my rootViewController
    Code:
    #pragma mark - Segue
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        
        if (![segue isKindOfClass:[rootSegue class]]) {
            [super prepareForSegue:segue sender:sender];
            return;
        }
        
        self.oldViewController = self.destinationViewController;
        
        //if view controller isn't already contained in the viewControllers-Dictionary
        if (![self.viewControllersByIdentifier objectForKey:segue.identifier]) {
            [self.viewControllersByIdentifier setObject:segue.destinationViewController forKey:segue.identifier];
        }
    
        self.destinationIdentifier = segue.identifier;
        self.destinationViewController = [self.viewControllersByIdentifier objectForKey:self.destinationIdentifier];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:rootViewControllerChangedNotification object:nil];
    }
    
    - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
        if ([self.destinationIdentifier isEqual:identifier]) {
            //Dont perform segue, if visible ViewController is already the destination ViewController
            [[NSNotificationCenter defaultCenter] postNotificationName: rootControllerViewControllerAlreadyVisibleNotification object:nil];
            return NO;
        }
        
        return YES;
    }
    
    header file
    Code:
    @interface rootViewController : UIViewController
        @property (weak,nonatomic) UIViewController *destinationViewController;
        @property (strong, nonatomic) NSString *destinationIdentifier;
        @property (strong, nonatomic) UIViewController *oldViewController;
    
        @property (weak, nonatomic) IBOutlet UIView *container;
    @end
    
    And my rootSegue
    Code:
    - (void) perform {
        rootViewController *newViewcontroller = (rootViewController *)self.sourceViewController;
        UIViewController *destinationViewController = (UIViewController *) newViewcontroller.destinationViewController;
        
        //remove old viewController
        if (newViewcontroller.oldViewController) {
            [newViewcontroller.oldViewController willMoveToParentViewController:nil];
            [newViewcontroller.oldViewController.view removeFromSuperview];
            [newViewcontroller.oldViewController removeFromParentViewController];
        }
        
        
        destinationViewController.view.frame = newViewcontroller.container.bounds;
        [newViewcontroller addChildViewController:destinationViewController];
        [newViewcontroller.container addSubview:destinationViewController.view];
        [newViewcontroller didMoveToParentViewController:newViewcontroller];
    }
    
    Its very basic for now.
     
  2. AppSwage, Feb 10, 2015
    Last edited by a moderator: Feb 11, 2015

    AppSwage macrumors newbie

    Joined:
    Jan 18, 2015
    #2
    If you want to communicate with the root controller you can do so via delegation or object notification (NSNotificationCenter). Either can have a method fire in the root from another view such as the container view.
     
  3. DennisBlah, Feb 10, 2015
    Last edited by a moderator: Feb 11, 2015

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    I'm not sure what you mean with the NotificationCenter.
    Actually what I need is a >public< function in the rootViewController
    Which I can call. By defining the function in the header I might be able to call it from the homeView if I delegate the rootView in homeView.

    But I don't think its the right approach for this. And proberbly need a different way like you mean with the notificationcenter
     
  4. DennisBlah, Feb 11, 2015
    Last edited by a moderator: Feb 11, 2015

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #4
    I allocated the rootViewController in the homeViewController, and tried to perform a public function:
    Code:
    -(void)performSegueFromRoot:(NSString *ident);
    This just triggers:
    Code:
    [self performSegueWithIdentifier: ident sender: self];
    But then from the homeViewController I get the error that rootViewController has no segue with the identifier I supply to it...
    But rootViewController does have the segue..

    Can anyone help me out here ?

    I could make another 'rootViewController' from the home, or use another container view in the homeView, but thats a cheap workaround for what Im looking for.
     
  5. Ubuntu, Feb 11, 2015
    Last edited by a moderator: Feb 11, 2015

    Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #5
    Select the respective segue between the two viewControllers in the Storyboard and make sure that the identifier is ident.
     
  6. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #6
    As you see in the function, 'ident' is an variable, which will contain any string I put in there.
    I check, and checked, and double checked. My 2nd segue is gotoTestView
    The identifier of the segue from rootView to the testView is gotoTestView

    When calling from rootViewController itself it works fine.
    When calling the function from rootViewController from the homeView it says
    rootViewController does not have segue 'gotoTestView'

    in homeView :
    Code:
    rootViewController *myRoot = [rootViewController alloc];
    [myRoot performSegueFromRoot: @"gotoTestView"];
    
    p.s. its a public function in rootViewController
     
  7. thefredelement macrumors 65816

    thefredelement

    Joined:
    Apr 10, 2012
    Location:
    New York
    #7
    Are the 'container's just UIViews?

    I'm a little confused about the behavior of your initial view controller.

    I get what you're trying to do, you're performing a segue from one view controller but you want it to perform as if from another view controller?

    This whole thing sounds like it would be better suited with UIViews that you can either use from storyboard or write out with a .xib and using some code to jump between them.

    Can you reply with a screenshot of your storyboard?
     
  8. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #8
    My rootViewController has a container view
    By default it loads homeView

    Now when using the buttons in the 'footer' of rootviewcontroller i can go through my views

    Now the thing is, I want to be able to change the view in my roots viewcontroller by code inside my homeview

    So I made a public function in rootView to trigger the segue's (thats also used for buttons in the footer)

    But when calling the function from my homeview it says that rootviewcontroller does not has that segue
     

Share This Page