[apple文檔]UIViewController編程指南

來源:互聯網
上載者:User
文章目錄
  • 一.View Controller Classes
  • 二.自訂UIVIewController
  • 三.View Controller的生存周期
  • 四.支援介面旋轉
  • 五.view顯示相關的通知
  • 六.viewController顯示(關閉)其它viewController
  • 七.controller的edit mode
一.View Controller Classes

二.自訂UIVIewController

1.建立

a)nib檔案

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];        // Override point for customization after application launch.        self.firstViewController = [[[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil] autorelease];        self.window.rootViewController = self.firstViewController;        self.firstViewController.wantsFullScreenLayout=YES;        [self.window makeKeyAndVisible];        return YES;}

如果你自訂controller實現initWithCoder:方法的話則會調用,如果沒有實現,則調用init.
在調用完後,架構會自動調用controller中的objects(一般是UIView)的awakeFromNib方法,讓objects可以有機會來初始化自己.

b)手動建立

只需要alloc&init方式就可以了

2.初始化

一般在controller中實現loadView方法來實現第一次的controller內容的管理,值得注意的是,如果自己實現了這個方法,則不需要調用super的loadView方法

3.設定root view controller的尺寸

a)window的尺寸

b)是否有status bar

c)裝置的方向(橫向豎向)

d)root view controller的wantsFullScreenLayout屬性。這個屬性就是設定是否要包含狀態列20像素,比方說你在view裡設定了一個button在頂部,這個如果是true,則button出現在狀態列下,如果是false,則會自動頂下去。本身並不影響status bar的隱藏和消失。只是決定status bar下面是否屬於程式繪製地區而已。

4.布局

首先可以利用每個view的autoresizingMask的設定來自動布局

viewcontroller布局改變的順序

a)view controller的vew尺寸改變

b)調用controller的viewWillLayoutSubview.

c)調用view的layoutSubview

d)調用controller的viewDidLayoutSubview.

三.View Controller的生存周期

1.初始化方法:init,initWithCoder;

2.載入controller's view:loadView: controller.view=nil; viewDidLoad: controller.view=view;

3.當收到didREceiveMemoryWarning的時候會調用viewWillUnload,viewDidUnload;

4.dealloc:釋放資源(ARC可以忽略)

四.支援介面旋轉

1.聲明支援的旋轉方向

shouldAutorotateToInterfaceOrientation:決定支援的旋轉方向(UIInterfaceOrientationIsLandscape的橫向2個方向 UIInterfaceOrientationIsPortrait豎直2個方向)

2.如何處理方向改變

a)方向發生改變

b)調用shouldAutorotateToInterfaceOrientation查看支援的方向

c)調用controller的willRotateToInterfaceOrientation:duration方法

d)觸發view的自動布局(詳細的看第二部分的第4點:布局)

e)調用didRotateFromInterfaceOrientation方法

3.為每個方向建立不同的介面

利用notification來通知不同的狀態。

@implementation PortraitViewController- (void)awakeFromNib{    isShowingLandscapeView = NO;    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];    [[NSNotificationCenter defaultCenter] addObserver:self                                 selector:@selector(orientationChanged:)                                 name:UIDeviceOrientationDidChangeNotification                                 object:nil];} - (void)orientationChanged:(NSNotification *)notification{    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&        !isShowingLandscapeView)    {        [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];        isShowingLandscapeView = YES;    }    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&             isShowingLandscapeView)    {        [self dismissViewControllerAnimated:YES completion:nil];        isShowingLandscapeView = NO;    }}

4.處理旋轉的小貼士

a)暫時禁止任何事件響應

b)儲存已經顯示的地圖地區(地圖應用中)

c)如果介面的view層次太過複雜而造成延遲,建議先做view的,相關方法我其它部落格文章中有提到

d)旋轉後,對於tableView的話,需要reload重新讀取資料。

e)使用旋轉通知來更新應用資訊。

五.view顯示相關的通知

1.方法有viewWillAppear: viewDidAppear: viewWillDisappear: viewDidAppear:

2.注意這個是controller.view被設定值或者controller.view被設定成nil調用的,當然這個值未必就是直接設定的,可能是通過controller的顯示和移除的。

3.知道view顯示移除的原因,在上述4個方法中調用isMovingFromParentViewController,isMovingToParentViewController,isBeingPresented,isBeingDismissed 。

六.viewController顯示(關閉)其它viewController

在5.0之前,對應的方法是使用model view controller系列的方法。5.0以後增加了presented,prensentingViewController的概念,modalViewController對應5.0後的presentedViewController

        FCDemoViewController *controller= [[FCDemoViewController alloc]initWithNibName:@"FCDemoViewController" bundle:nil];        controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;        [self presentModalViewController:controller animated:YES];        NSLog(@"%@",self.modalViewController);        //5.0         [self presentViewController:controller animated:YES completion:nil];        NSLog(@"%@",self.presentedViewController);        NSLog(@"%@",self.presentingViewController);

屬性modalPresentationStyle在iPad下有幾種顯示形式,對應不同的顯示地區。屬性modellTransitionStyle決定過渡形式.

關閉也有對應的方法:dismissModalViewControllerAnimated或iOS5.0以後的dismissViewControllerAnimated:completion:

5.0以後其它的變化:

controller可以自訂子controller的集合,這樣每一個controller都可以是一個container controller.

definesPresentationContext決定當前顯示其它controller的controller是否提供context給presentedViewController(modalViewController),如果不,就會找上一級的controller的該值。

詳細的操作可以查看class reference.

蘋果官方推薦使用協議的方式來讓controller相互連信。首先聲明一個協議,並在主controller中實現該協議的方法,在顯示其它controller的時候,為其設定delegate=self.這樣在其它controller需要回調presentingViewController就可以直接用delegate方法來回調到它。通過這樣的方式,可以使得複用性大大增強。而且被顯示的controller也不用完全知道顯示它的controller的所有資訊和屬性。

七.controller的edit mode

1.當設定controller的editing屬性,會自動觸發setEditing:animated屬性,這個時候通過myController editButtonItem獲得的編輯按鈕會自動從edit變成Done,這個通常使用在navigation Controller

比如設定一個右上方按鈕為editButton的話代碼如下

myViewController.navigationItem.rightBarButtonItem=[myViewController editButtonItem];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.