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生命週期 ![](https://github.com/zt1991616/blog/raw/master/Image/Load.jpg) ```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);
}
}
```動畫