Instantiate Storyboard ViewControllers Manually

Quote

I’ve started using storyboards in XCode/iOS more frequently, but I really needed something different from the normal segues and connections that Apple provides, and while Apple briefly documents how to do this, I thought I’d share my example.  Here is the view in Interface Builder of the test app that I created:

My initial controller is on the left, and the blue area is simply a UIView that I’ve connected to  a member variable outlet named _parentView.  This is where I want my yellow and pink sub views to appear, each handled by their own controller.  Notice that there is no connection between the storyboards in this case.  The key here is to set the Storyboard ID for yellow and pink View Controllers so I can instantiate them in my code.  If I don’t set a storyboard id for the yellow and pink View Controllers, I’ll get a warning from XCode that they are unreachable. You set the Storyboard ID in the Identity inspector (see the inspector panel on the right in the picture above – make sure ‘Use Storyboard ID’ is checked).

Once I have storyboard IDs set for each of these view controllers, the code is quite simple to instantiate the view controllers and embed them using the parentView as a container.

- (IBAction)showView1:(id)sender 
{
  [self switchViews:@"yellow"];
}

- (IBAction)showView2:(id)sender 
{
  [self switchViews:@"pink"];
}

- (void)switchViews:(NSString*)storyboardId
{
  __block UIViewController *lastController = _childController;

  _childController = (UIViewController*)[self.storyboard instantiateViewControllerWithIdentifier:storyboardId];
  [self addChildViewController:_childController];
  [_parentView addSubview:_childController.view];

  CGRect parentRect = _parentView.bounds;
  parentRect.origin.x += parentRect.size.width;
  _childController.view.frame = parentRect;
  _childController.view.alpha = 0;
  [UIView animateWithDuration:0.5 animations:^{
    _childController.view.frame = _parentView.bounds;
    _childController.view.alpha = 1.0;
  } completion:^(BOOL finished) {
    if (lastController)
    {
      [lastController.view removeFromSuperview];
      [lastController removeFromParentViewController];
    }
  }];
}
The basics in the code above:
– Keep a pointer to the last view controller child
– Use instantiateViewControllerWithIdentifier using the storyboard id you set in interface builder to create and return the view controller.
– Add the new controller as a child controller to the parent
– Add the new controllers view as a subView to the parent
– Then for fun I animate the new controller onscreen
– When the new view is fully animated, then I remove the old view
With this basic flow, you can customize the user interface in just about any way you can imagine.

Welcome to Average Pro

I’ve followed a number of interesting blogs from a wide range of software engineers, and finally decided that I would add my voice. I’ve been lucky enough to have a wide range of experiences in software engineering and business startup, and hope that some of the things I post might be useful to others.

What really inspired me was my wife coming up with the name Average Pro while trying to come up with a company name.  I didn’t think it was right for a company name, but thought it was perfect for my blog.  It’s how I see myself, as an average professional.

So what makes me qualified to share my experiences and thoughts (other than being a living being like anyone else)?  I’ve been writing software for 33 years (since I was 14, I’ll let you do the math).

I started my career after high school working for a hotel software company.  During college I sold Amiga computers and wrote a number of general-purpose and video-related programs (this was before they were called apps of course). After college I worked for a company that created factory and warehouse automation software.

In 1993, I co-founded Hollywood FX, which was a small but profitable company that made 3D video effects plugins for non-linear editing software.  We sold Hollywood FX to Pinnacle Systems in 1999, where we integrated Hollywood FX and our content licensing software into the Pinnacle Studio product line. Pinnacle was purchased by Avid in 2005, and I continued to work there, creating a new 3D video processing engine, a new animation system, Motion Titler, and much more.

In July of 2012, Avid sold its consumer video division to Corel.  I stayed with Corel for 5 months, and decided it was time to move on.  In November 2012, I joined Condition One, an exciting startup that’s created a unique immersive video technology.

During these many years I’ve learned an incredible amount, and found that there is still an incredible amount to learn.  This blog is my effort to share what I’ve learned both in software engineering and business.  I hope you’ll find something of value.