IPhone UIStoryBoard can I programmatically create a controller with the interface defined in the storyboard?

I have a bunch of view controllers defined in UIStoryBoard. I like to have all of them in one place for easy access. However, I ran into a problem when I needed to create an instance of the view controller elsewhere in the application. Is it possible to programmatically create a UIViewController using the interface for it that is defined in the storyboard?

Otherwise, I would have to copy the storyboard view for this controller into a separate .xib file and load it manually.

Thanks for clarifying!

+3
source share
1 answer

Yup, ! -instantiateInitialViewController - , .

, :

- (void) loadStoryboard:(NSString *)storyboardName animated:(BOOL)animated
{
  if ([_currentStoryboard isEqual:storyboardName])
  {
    return;
  }

  _currentStoryboard = storyboardName;

  UIStoryboard* storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
  UIViewController* newRootController = [storyboard instantiateInitialViewController];

  if (!animated)
  {
    self.window.rootViewController = rootController;
    return;
  }

  newRootController.view.alpha = 0.0;
  [self.window addSubview:newRootController.view];

  [UIView animateWithDuration:0.5 animations:^{
    newRootController.view.alpha = 1.0;
  } completion:^(BOOL finished) {
    self.window.rootViewController = newRootController;
  }];
}

, AppDelegate, .

+4