Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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.
 
Last edited:

AppSwage

macrumors newbie
Jan 18, 2015
29
0
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.
 
Last edited by a moderator:

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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.

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
 
Last edited by a moderator:

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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.
 
Last edited by a moderator:

Ubuntu

macrumors 68020
Jul 3, 2005
2,140
474
UK/US
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.

Select the respective segue between the two viewControllers in the Storyboard and make sure that the identifier is ident.
 
Last edited by a moderator:

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Select the respective segue between the two viewControllers in the Storyboard and make sure that the identifier is ident.

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
 

thefredelement

macrumors 65816
Apr 10, 2012
1,193
646
New York
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?
 

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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?

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.