1

I have 3 different buttons which upon touch present the same UINavigationViewController with a container. However, each button represents which view controller will be embed at the container.

How can I embed the necessary viewController by code?

Blisskarthik
  • 1,246
  • 8
  • 20
Gal Marom
  • 8,499
  • 1
  • 17
  • 19

2 Answers2

0

You should implement prepareForSegue method:

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }

id is your button that will fire segue. You do segues by drugging and drop in storyboard. Put an if statement in this method and tell your UIViewController which UIView to load in container. You can pass data like this:

// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];

// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object]; 

Second snippet taken from this answer.

Update.

To load different UIView in container put if statement into viewWillApper method.

This method firing earlier than viewDidLoad. If statement must check some property that tell what UIView to init. You setting up this property in prepareForSegue.

It will look like this:

if (self.viewToLoad == 1)
{
    self.dynamicView = MyCustonUIViewNumberOne *view = [MyCustonUIViewNumberOne alloc] init]; 
}

Update 2.

Or you can do it dynamically like in this answer:

if (self.viewToLoad == 1)
{
    // Replacing with your dimensions
    CGRect frame = CGRectMake(x, y, width, height);
    MyCustonUIViewNumberOne *dynamicView = [[MyCustonUIViewNumberOne alloc] initWithFrame:frame];
    [self.container addSubview: dynamicView];
} else {
    // Init other view
}

The container property:

enter image description here

Community
  • 1
  • 1
Boris Y.
  • 4,387
  • 2
  • 32
  • 50
  • it doesn't answer my question – Gal Marom Jul 23 '14 at 14:55
  • @GalMarom I updated my answer. Please don't hesitate to ask more question. – Boris Y. Jul 23 '14 at 15:03
  • what is self.container? – Gal Marom Jul 23 '14 at 16:00
  • @GalMarom I tried to play in Xcode with it. I change container.view to dynamicView. It so more correct. dynamicView is any view inside your container. Here is container https://www.dropbox.com/s/x6ijbpb6ttvm0p8/Screenshot%202014-07-23%2020.13.07.png and dynamicView https://www.dropbox.com/s/dmh29dhyzj2lpaw/Screenshot%202014-07-23%2020.13.02.png – Boris Y. Jul 23 '14 at 16:13
  • @GalMarom If my solution with property won't work, you can create UIView on the fly and add it to your controller. I will research this and update my answer if you need it. – Boris Y. Jul 23 '14 at 16:16
  • It's not good - because it's only add the subview and not behave as a view controller – Gal Marom Jul 24 '14 at 11:47
0

what you can do is use an identifier which would be assigned to your various viewController as storyboardID such as fisrtVC, SecondVC, thirdVC

the depending upon whichButton is pressed just set the identifier and use this identifier when you want to push the controller such as

for example

while you push the navigation viewController just pass the storyboard Identifier such as

Declare a NSString *identifier;

-(IBAction)firstButtonClicked{
identifier=@"firstVC";
//pass this identifier to your navigationController
}

similarly for other Controllers

When you push the navigation controller make sure to pass this identifier along now depending upon the value you can initiate the controller on you VC as

on ViewDIdApppear:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    NSString* viewType = passedIdentifier
   UIViewCOntroller* viewController = [storyboard instantiateViewControllerWithIdentifier:viewType];

Load this "viewCOntroller in your ContainerView"

Geet
  • 2,427
  • 2
  • 21
  • 39