04_视图控制器
来源:互联网 发布:新纪元取数软件 编辑:程序博客网 时间:2024/06/05 15:51
<pre name="code" class="objc">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; /* 视图控制器 loadView 方法调用的时机 1.调用了view的访问器 2.视图控制器的view为空 */ RootViewController *rootCtrl = [[RootViewController alloc] init]; //调用了view的访问器,并且view为nil,此时会调用loadView方法 UIView *view = rootCtrl.view;// rootCtrl.view.backgroundColor = [UIColor grayColor]; //不推荐直接将view添加到window上// [self.window addSubview:rootCtrl.view]; //给windown设置根视图控制器 /* 取得视图控制器的视图,添加到window上去显示 */ self.window.rootViewController = rootCtrl; return YES;}
// [[NSBundle mainBundle] loadNibNamed:@"" owner:<#(id)#> options:<#(NSDictionary *)#>]/* 视图控制器的xib文件,是在UIViewController的loadView中加载此xib文件的 如果此控制器的视图右xib创建的,则不要覆写loadView方法,这样会导致xib无法加载 *///- (void)loadView {// // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];// self.view = view;// [view release];// //}
- (void)viewDidLoad{ [super viewDidLoad]; subView1 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)]; subView1.backgroundColor = [UIColor redColor]; [self.view addSubview:subView1]; subView2 = [[UIView alloc] initWithFrame:CGRectMake(10, 160, 100, 50)]; subView2.backgroundColor = [UIColor greenColor]; [self.view addSubview:subView2]; subView3 = [[UIView alloc] initWithFrame:CGRectMake(10, 300, 100, 50)]; subView3.backgroundColor = [UIColor grayColor]; [self.view addSubview:subView3]; }/* 6.0之前使用shouldAutorotateToInterfaceOrientation方法来控制方向 6.0之后使用supportedInterfaceOrientations方法来控制方向 *///6.0之前设备旋转调用的方法//参数:toInterfaceOrientation 表示当前设备所处的方向/*- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { if (toInterfaceOrientation == UIInterfaceOrientationPortrait | toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ) { if (toInterfaceOrientation == UIInterfaceOrientationPortrait) { subView1.frame = CGRectMake(10, 10, 100, 50); subView2.frame = CGRectMake(10, 160, 100, 50); subView3.frame = CGRectMake(10, 300, 100, 50); } else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) { subView1.frame = CGRectMake(10, 10, 100, 50); subView2.frame = CGRectMake(10, 100, 100, 50); subView3.frame = CGRectMake(10, 200, 100, 50); } return YES; } return NO;} *///6.0之后设置当前控制器界面所支持的方向- (NSUInteger)supportedInterfaceOrientations { //获取到状态栏的方向,也就是设备的方向 UIInterfaceOrientation interface = [UIApplication sharedApplication].statusBarOrientation; if (interface == UIInterfaceOrientationPortrait) { subView1.frame = CGRectMake(10, 10, 100, 50); subView2.frame = CGRectMake(10, 160, 100, 50); subView3.frame = CGRectMake(10, 300, 100, 50); } else if(interface == UIInterfaceOrientationLandscapeLeft) { subView1.frame = CGRectMake(10, 10, 100, 50); subView2.frame = CGRectMake(10, 100, 100, 50); subView3.frame = CGRectMake(10, 200, 100, 50); } //返回朝向的枚举值 return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;}
/* Appear这组方法的调用时机: 当前控制器视图被添加到另外一个视图上显示时调用: [self.view addSubview:modalCtrl.view]; *///此控制器的视图将要出现在屏幕上时调用- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"viewWillAppear");}//此控制器的视图已经出现在屏幕上时调用- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"viewDidAppear");}//-------------------------------------------/* Disappear这组方法调用的时机: 当前视图控制器视图被移除时调用 [self.view removeFromSuperview]; *///视图将要消失时调用- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; NSLog(@"viewWillDisappear"); }//视图已经消失时调用- (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; NSLog(@"viewDidDisappear"); }
0 0
- 04_视图控制器
- UI笔记_视图控制器UIViewController
- iOS开发入门_视图控制器_UI_02
- 04 视图控制器
- UI 07 _ 导航视图控制器 与 属性传值
- 控制器 & 视图
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- 视图控制器
- poj 3190
- C#学习之interface
- Cookie/Session机制详解
- ubuntu脚本自动输入sudo密码
- 什么是Service, 以及Service 模板
- 04_视图控制器
- 使用dygraphs,动态刷新线图
- AngularJS介绍
- Objective-C 词典与集合
- java开发微信支付(刷卡支付)
- 存储过程 例子
- 如何设置文件夹可写权限
- 树莓派2B安装配置
- XML学习1