Creating Custom View Controller Objects at Runtime
MyViewController* vc = [[MyViewController alloc] initWithNibName:@"MyViewController"
bundle:nil];
- (id)initWithData:(NSArray*)data {
if ((self = [super initWithNibName:@"MyViewController" bundle:nil])) {
// Initialize the view controller with the starting data
}
return self;
}
2. When creating view controllers programmatically, it is your responsibility to set the frame of the view controller’s view appropriately before using the view. A programmatically created view controller that loads its view from a nib file does not try to change the size or position of that view. If the view controller is presented modally or used with a container view controller, the parent or container view controller often adjusts the view for you. But in cases where you add the view to a window yourself, the view’s existing frame is used as is. If your application has a status bar, failing to adjust the view’s frame could cause the misplacement of the view underneath the status bar.
Presenting a View Controller’s View
1. There are several options for displaying the view associated with a view controller:
- Display the view directly by adding it to a window using theaddSubview: method.
- Display the view indirectly using one of the following techniques:
- Present the owning view controller modally using thepresentModalViewController:animated: method.
- Push the owning view controller onto the navigation stack of a navigation controller object.
- Make the owning view controller the root view controller of a tab in a tab bar interface.
- On iPad, present the view controller using a popover.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// At this point, the main nib file is loaded.
// It is a good idea to set the view's frame before adding it to a window.
[viewController.view setFrame:[[UIScreen mainScreen] applicationFrame]];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
2. If you plan to add a view to a window using the addSubview: method, it is recommended that you explicitly set the frame of the view after loading it from a nib file. If you plan to present a view modally or use it in conjunction with a container view controller, you do not need to set the frame explicitly.
Responding to Display-Related Notifications
Adopting a Full-Screen Layout for Custom Views
1. If your application displays a translucent status bar, you can set the value of your view controller’s wantsFullScreenLayout property to YES to allow your view to underlap the status bar.
Enabling Edit Mode for a View
1. If you want to use the same view controller to display and edit content, you can override the setEditing:animated: method and use it to toggle the view controller’s view between display and edit modes.
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
[super setEditing:flag animated:animated];
if (flag == YES){
// change views to edit mode
}
else {
// save the changes if needed and change views to noneditable
}
}
Handling Events
Accessing Related View Controller Objects
A custom view controller is responsible for providing any specific objects needed by higher-level view controllers.
If you do not specify a custom object for any of these properties, the view controller provides an appropriate default item for you.