Loading a custom UIView in the main UIViewController view

I subclassed UIView and created a NIB that controls the core logic for my application.

Hoping that the presentation will scale well, I want to use it for both the iPhone and iPad versions of the application.

On the iPhone, the view will cover the entire screen. On iPad, the view will cover only part of the screen.

I read that you should not use UIViewControllers to control only part of the screen. So, I'm trying to embed a custom UIView in the main view of the UIViewController using IB.

How can I do that?

+4
source share
2 answers

After a lot of trial and error, I found a solution based on the approach described below by question , which was answered by Brian Webster .

The solution was originally proposed for the Cocoa environment. I hope this is valid in the iOS environment.

  • Create the main view controller with the NIB file. In NIB, the file owner must match the class of your primary view controller.
  • Create a custom view controller with a NIB file. In this NIB, the file owner must match the class of your custom view controller.
  • Create your own view controller property in your master view controller class.
  • Create the UIView property in the controller class of the main view. It will store your custom controller view. Define it as an IBOutlet, so it can be linked in the NIB.
  • Drop the UIView into your primary NIB controller. Associate it with the main view of the IBOutlet controller controller. It will be used as a placeholder for a custom view.
  • In the viewDidLoad method of the main view of the controller, load custom NIB view controllers, determine the size of the custom view size, and copy the view in the main view controller view.

Here is the code:

  • MainViewController.h

    @interface MainViewController : UIViewController { CustomViewController *customViewController; UIView *customView; } @property (nonatomic, retain) CustomViewController *customViewController; @property (nonatomic, retain) IBOutlet UIView *customView; @end 
  • MainViewController.m

     - (void)viewDidLoad { CustomViewController *controller = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil]; self.customViewController = controller; [controller release]; customViewController.view.frame = customView.frame; customViewController.view.autoresizingMask = customView.autoresizingMask; [customView removeFromSuperview]; [self.view addSubview:customViewController.view]; self.customView = customViewController.view; [super viewDidLoad]; } 
+6
source
  • Add the IBOutlet property for your custom UIView to the UIViewController and additional outputs for any subzones that you want to access.
  • Go to Interface Builder, select the "File Owner" object in your NIB, and in the Inspector go to the topmost tab, setting your class to match your UIViewController class.
  • Connect IBOutlet from the first step to "File Owner" to your custom UIView.
  • In Xcode, when you need to load your view, do something like this:

-

  [[NSBundle mainBundle] loadNibNamed:@"MyNib" owner:self options:0]; self.myCustomView.frame=self.view.bounds; // make view fill screen - customize as necessary [self.view addSubview:self.myCustomView]; 

When loading the NIB, the sockets installed in step 1 will be filled with objects downloaded from your NIB.

+1
source

All Articles