首页 > 代码库 > iOS,视图控制器相关(UIViewController)
iOS,视图控制器相关(UIViewController)
1.视图控制器各个方法调用时机
2.选项卡(Tab Bar)和导航栏(Navigation Bar)
视图控制器各个方法调用时机
init:方法
选项卡(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:@"tab_message@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_message_select@3x.png"] 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_ contact@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_ contact_select@3x.png"] 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_ dynamic@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tab_ dynamic_select@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
navDyn.tabBarItem=dynTabItem;
self.tabBarController=[[UITabBarController alloc] init];
self.tabBarController.viewControllers=@[navMsg,navCon,navDyn];
self.tabBarController.selectedIndex=0;
//由于iPhone是单窗口程序,所以也就只有这么一个Window对象,而且是UIWindow,不是NSWindow。而根据文档上所说:这个是便捷方法,去使被使用对象的主窗口显示到屏幕的最前端。
[self.window makeKeyAndVisible];
return YES;
}
iOS,视图控制器相关(UIViewController)