View Controller

Each controller has a view that gets placed on the window. (The view often has subviews like buttons and labels.) Thus, we call these controllers view controllers.

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
[[self window] setRootViewController:hvc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

 

A view controller is a subclass of UIViewController and has an instance variable called view.

When using view controllers, you don't have to manipulate the view hierarchy directly. UIWindow implements a method named setRootViewController:.Passing an instance of UIViewController as the argument to this method automatically adds the view of that view controller as a subview of the window and resize it to fit. The window also retains its root view controller.

// Create the tabBarController
UITabBarController *tabBarController = [[UITabBarControlleralloc] init];
// Set tabBarController as rootViewController of window
[[self window] setRootViewController:tabBarController];

UIViewController's designated initializer

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
// Call the superclass's designated initializer
self = [super initWithNibName:nil
bundle:nil];
if (self) {
// Get the tab bar item since currently superview is TabBar...
UITabBarItem *tbi = [self tabBarItem];
// Give it a label
[tbi setTitle:@"Time"];
}
return self;
}

 

 

 The view controller lifecycle and low memory warnings

A view controller, like any other object, is created through alloc and init. It does not, however, create its view at that time.Instead, it waits until the view is really needed before it calls loadView.

@implementation HypnosisViewController
- (void)loadView
{
// Create a view
CGRect frame = [[UIScreen mainScreen] bounds];
HypnosisView *v = [[HypnosisView alloc] initWithFrame:frame];
// Set it as *the* view of this view controller
[self setView:v];
}@
end

 

How does a view controller know when to load its view? When it is sent the message view.

- (UIView *) view
{
    if([self isViewLoaded] ==NO)
    {
        [self loadView];
        [self viewDidLoad];
    }

     return view;
}

When the system is running low on RAM, it issues a low-memory warning to the running application. The application responds by freeing up any resources that it doesn’t need at the moment and can easily recreate. View controllers, during a low-memory warning, are sent the message
didReceiveMemoryWarning. The default implementation of this method will check to see if the view is currently on screen; if it is not, it is released. (If the view is on screen, nothing happens.)

 

When a low-memory warning occurs, CurrentTimeViewController's view is destoryed- but  the instance of CurrentTimeViewController is not. When you switch back to the Time tab, the view is recreated, but the CurrentTimeViewController itself is not recreated.

So we can make a rule out of this: never access a view controller's view in that view contoller's initialization method. Since view controller and view have different lifycycle. If you have extra work you want to perform on the view, do so in viewDidLoad. This message is sent to a view controller each time it loads its view.

posted on 2012-06-14 09:59  grep  阅读(281)  评论(0编辑  收藏  举报