1.视图控制器各个方法调用时机
2.选项卡(Tab Bar)和导航栏(Navigation Bar)
视图控制器各个方法调用时机
init:方法
在init方法中实例化必要的对象(遵从LazyLoad思想)
init方法中初始化ViewController本身
loadView:方法
这是当没有正在使用nib视图页面,子类将会创建自己的自定义视图层。绝不能直接调用
viewDidLoad:方法
在视图加载后被调用
viewWillAppear:方法
视图即将可见时调用
viewDidAppear:方法
视频完全过渡到屏幕上时调用
viewWillDisappear:方法
视图被驳回时调用,覆盖或以其他方式隐藏
viewDidDisappear:方法
视图被驳回后调用,覆盖或以其他方式隐藏
当A视图切换到B视图时,顺序流程
1.B视图的viewDidLoad
2.A视图的viewWillDisappear
3.B视图的viewWillAppear
4.A视图viewDidDisappear
5.B视图viewDidAppear
选项卡(Tab Bar)和导航栏(Navigation Bar)
//AppDelegate.h文件
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic) UITabBarController *tabBarController;//底部选项卡控制器
@end
//AppDelegate.m文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
//消息控制器
MessageViewController *msgController=[[MessageViewController alloc] init];
UINavigationController *navMsg=[[UINavigationController alloc] initWithRootViewController:msgController];
//选项卡的图片样式以这种方式处理,避免图片无法完全显示
UITabBarItem *msgTabItem = [[UITabBarItem alloc]initWithTitle:@"消息" image:[[UIImage imageNamed:@"[email protected]"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"[email protected]"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
navMsg.tabBarItem=msgTabItem;
//联系人控制器
ContactViewController *conController=[[ContactViewController alloc] init];
UINavigationController *navCon=[[UINavigationController alloc] initWithRootViewController:conController];
UITabBarItem *conTabItem = [[UITabBarItem alloc]initWithTitle:@"联系人" image:[[UIImage imageNamed:@"tab_ [email protected]"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_ [email protected]"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
navCon.tabBarItem=conTabItem;
//动态控制器
DynamicViewController *dynController=[[DynamicViewController alloc] init];
UINavigationController *navDyn=[[UINavigationController alloc] initWithRootViewController:dynController];
UITabBarItem *dynTabItem = [[UITabBarItem alloc]initWithTitle:@"动态" image:[[UIImage imageNamed:@"tab_ [email protected]"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_ [email protected]"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
navDyn.tabBarItem=dynTabItem;
self.tabBarController=[[UITabBarController alloc] init];
[email protected][navMsg,navCon,navDyn];
self.tabBarController.selectedIndex=0;
//由于iPhone是单窗口程序,所以也就只有这么一个Window对象,而且是UIWindow,不是NSWindow。而根据文档上所说:这个是便捷方法,去使被使用对象的主窗口显示到屏幕的最前端。
[self.window makeKeyAndVisible];
return YES;
}