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.

转载于:https://www.cnblogs.com/grep/archive/2012/06/14/2548860.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值