UINavigationController
,标签栏控制器UITabBarController
,表视图控制器UITableViewController
等```Objective-C
// 代码建立
UIViewController *mainViewController = [[UIViewCtroller alloc] init];
mainViewController.view.backgroundColor = [UIColor redColor];
self.window.rootViewController = mainViewCtroller;
// nib建立
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"view" bundle:nil];
self.window.rootViewController = rootViewController;git
##UIViewController生命周期  ```Objective-c -(void)loadView { // 调用父类来建立view // 从nib、storybord加载View,不然建立一个empty view // 建立一个自定义的视图,覆盖便可 } -(void)view
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - (void)loadView - (void)viewDidLoad - (void)viewWillAppear:(BOOL)animated - (void)viewDidAppear:(BOOL)animated - (void)viewWillDisappear:(BOOL)animated - (void)viewDidDisappear:(BOOL)animated
- iOS6以前使用viewDidUnload:
方法来释放对象的引用
- iOS6以后使用didRecevelMemoryWarning
内存紧张的时候调用
1. viewWillDisappear 视图将被从屏幕上移除以前执行
2. viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图
3. dealloc 视图被销毁,此处须要对你在init和viewDidLoad中建立的对象进行释放github
```Objective-c
-(void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundeRect];
button.frame = CGRectMake(320/2 - 140/2,80,140,40);
[button setTitle:@"Present" forState:UIControlStateNormal];
[button addTarget:self action:@selector[presentModalVc] forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)presentModalVC
{
ModalViewController *modalVc = [[ModalViewController alloc] init];
// 动画效果
modalVc.modalTranstionSyle = UIModalTransitionStylePatialCurl;
if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
[self.presentModalViewController:modalVc animated:YES];
}else{
[self.presentModalViewController:modalVc animated:YES completion:^{
NSLog(@"call back");
}];
[modalVC relese];
}
}设计模式
```Objective-c //ModalViewController.m -(void)dismiss { // 将模态视图关闭 [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"dismiss")]; }]; }
- iOS设备中的加速计能够肯定设备的当前方向。默认状况下,一个应用支持纵向和横向。当设备方向改变时,系统会发送UIDiviceOrientationDidChangeNotfication通知,默认状况下UIKit框架监听这个通知,并自定义更新这个方向。app
```Objective-c
-(void)viewDidLoad
{
// ...
// 采用通知获取屏幕方向切换
[[UINotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientation:)name:UIDeviceOrientationDidChangeNotifitation object:nil];
}
-(void)deviceOrientation:(NSNotification *)notification
{
UIDevice *device = (UIDevice *)[notification object];
NSLog(@"device:%d",device.orientation);
}
-(BOOL)shoudlAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return NO; //NO为不支持
// return (toInterfaceOrientation != UIterfaceOrientationLandscapeLeft); // 不支持一个方向
}框架
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrienttation)toInterfaceOrientation
{
return NO;
}ide
-(BOOL)shouldAutorotate
{
return YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"duration:%f",duration);
UIView* button = [self.view viewWithTag:101];
if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
button.frame = CGRectMake(320/2-140/2,80,140,40);
}else{
button.frame = CGRectMake(480/2-140/2,80,140,40);
}
}
```动画