0

In Storyboard I have a TabBarController set as the initial view controller which connects ("view controller" relationship) to a Navigation Controller, which in turn connects to a View controller (iphone5VC). How is it possible to programmatically change the view controller iphone5VC to iphone4VC? I have to decide which of iphone5VC or iphone4VC I will display depending on the phone size (iphone4, 5)

animuson
  • 53,861
  • 28
  • 137
  • 147
men
  • 131
  • 1
  • 9

3 Answers3

1

Thanks a lot to both of you. I finally decided to have only one Storyboard and use specific viewcontrollers on an adhoc basis when the 3.5 screen really needs to have a slightly different layout. What I did is:

Added <UITabBarControllerDelegate> in the viewcontroller .h file from which the user presses on the TabBar to select the view.

Added in the viewdidLoad of the .m file:

UITabBarController *tbc = self.tabBarController;
[tbc setDelegate:self];

and then added in the same file:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    UIStoryboard *myStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    CGSize result = [[UIScreen mainScreen] bounds].size;

    if (tabBarController.selectedIndex == 1)  // button # 2 pressed
    {
        if (result.height == IPHONE4_HEIGHT)
        {
            navController = (UINavigationController *) [myStoryBoard instantiateViewControllerWithIdentifier:@"ViewControlleriphone4"];
            [self presentViewController:navController animated:NO completion:nil];
        }
        else
        {
             navController = (UINavigationController *) [myStoryBoard instantiateViewControllerWithIdentifier:@"ViewControlleriphone5"];
            [self presentViewController:navController animated:NO completion:nil];
        }
    }
}
men
  • 131
  • 1
  • 9
0

If I'm reading your question correctly, you want to include a "check" in your app where the result will set the size of your view controllers, and I'm assuming the layout of the views contained within, based upon which phone the user has. If I have this correct, check out this thread. Also, I would strongly recommend using Auto Layout, which will automatically place the views inside your view controller, regardless of screen size and layout (portrait/landscape).

If I'm not understanding your question, maybe paste a screenshot or some code. Hope this helps, good luck!

Community
  • 1
  • 1
BrianS
  • 241
  • 2
  • 11
  • It is not only a matter of resizing the entire view. If the user iphone is 5, some my textviews can be larger for instance, and I want to design a slightly different viewcontroller with Storyboard when the device is iphone 4. The problem is that my design apparently does not allow me to "catch" the view before it is displayed and make a decision which one to push or present. – men Jan 21 '14 at 21:14
  • Ah, that makes sense. From reading his/her code above it looks like @MDB983 has a solution that works, though I did not test it. – BrianS Jan 22 '14 at 01:19
0

So if i am understanding you correctly, you really want to load a separate storyboard (and associated viewController)depending on the device type.

if so, what you need to do is have your main storyboard only contain the initial TabBarController, Navigation Controller, and a subclass of UIViewController we'll call dynamicViewController. The dynamicViewController will load the appropriate storyboard based on the device type. Obviously the various storyboards will need to exist (in the code below, the storyboards are named iphoneV4.storyboard and iphoneV5.storyboards)

So your dynamicViewController, simply needs the following -(void)viewDidLoad method;

 - (void)viewDidLoad {
    [super viewDidLoad];
    UIViewController __unused * targetViewController = nil;
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    UIStoryboard *targetStoryboard = (screenBounds.size.height == 568) ? [UIStoryboard storyboardWithName:@"iphoneV5" bundle:nil] : [UIStoryboard storyboardWithName:@"iphoneV4" bundle:nil];
    targetViewController = [targetStoryboard instantiateInitialViewController];

    if (self.parentViewController && ![self.parentViewController isKindOfClass:UITabBarController.class] && ![self.parentViewController isKindOfClass:UINavigationController.class]) {
    // replace self with the target view controller
      [self.parentViewController addChildViewController:targetViewController];
      [self.view.superview insertSubview:targetViewController.view aboveSubview:self.view];
      [self.view removeFromSuperview];
      [self removeFromParentViewController];
   } else { // tab bars, nav controllers, and modal dialogs
      [self addChildViewController:targetViewController];
      CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
      targetViewController.view.frame = f;
      [self.view addSubview:targetViewController.view];
  }
}
MDB983
  • 2,444
  • 17
  • 20