ios5中UIViewController新使用方法

來源:互聯網
上載者:User

在iOS5中,ViewController中新添加了下面幾個方法:

 
  1. addChildViewController:  
  2. removeFromParentViewController  
  3. transitionFromViewController:toViewController:duration:options:animations:completion: 
  4. willMoveToParentViewController:  
  5. didMoveToParentViewController: 

下面詳細介紹一下addChildViewController,一個ViewController可以添加多個子ViewController,但是這些子ViewController只有一個是顯示到父視圖中的,可以通過transitionFromViewController:toViewController:duration:options:animations:completion:這個方法轉換顯示的子視圖。同時加入相應的動畫。下面以一個例子來說明,最後實現的效果:

點擊其中的按鈕如下:

下面詳細介紹一下上述效果的實現:

3.把MainViewController添加到window中。

 
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     // Override point for customization after application launch.  
  5.     MainViewController *mainViewController=[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];  
  6.     self.window.rootViewController=mainViewController;  
  7.     [self.window makeKeyAndVisible];  
  8.     return YES;  

4.在MainViewController中添加三個按鈕,並且串連onClickbutton方法。

5.在MainViewController中添加三個子controller

 
  1. #pragma mark – View lifecycle - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib.  
  5.      
  6.     FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];  
  7.     [self addChildViewController:firstViewController];  
  8.      
  9.     SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];  
  10.     [self addChildViewController:secondViewController];  
  11.      
  12.      
  13.     ThirdViewController *thirdViewController=[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];  
  14.     [self addChildViewController:thirdViewController];  
  15.      
  16.     [contentView addSubview:thirdViewController.view];  
  17.     currentViewController=thirdViewController;  

其中要把其中的一個子controller的view添加到根視圖中,這樣才能顯示出相應的視圖。

6.點擊按鈕,切換視圖。

 
  1. -(IBAction)onClickbutton:(id)sender  
  2. {  
  3.     FirstViewController *firstViewController=[self.childViewControllers objectAtIndex:0];  
  4.     ThirdViewController *thirdViewController=[self.childViewControllers objectAtIndex:2];  
  5.     SecondViewController *secondViewController=[self.childViewControllers objectAtIndex:1];  
  6.     if ((currentViewController==firstViewController&&[sender tag]==1)||(currentViewController==secondViewController&&[sender tag]==2) ||(currentViewController==thirdViewController&&[sender tag]==3) ) {  
  7.         return;  
  8.     }  
  9.     UIViewController *oldViewController=currentViewController;  
  10.     switch ([sender tag]) {  
  11.         case 1:  
  12.         {  
  13.             NSLog(@"留言及回複");  
  14.             [self transitionFromViewController:currentViewController toViewController:firstViewController duration:4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{  
  15.             }  completion:^(BOOL finished) {  
  16.                 if (finished) {  
  17.                     currentViewController=firstViewController;  
  18.                 }else{  
  19.                     currentViewController=oldViewController;  
  20.                 }  
  21.             }];  
  22. }  
  23.             break;  
  24.         case 2:  
  25.         {  
  26.             NSLog(@"生日提醒通知");  
  27.             [self transitionFromViewController:currentViewController toViewController:secondViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{  
  28.                  
  29.             }  completion:^(BOOL finished) {  
  30.                 if (finished) {  
  31.                   currentViewController=secondViewController;  
  32.                 }else{  
  33.                     currentViewController=oldViewController;  
  34.                 }  
  35.             }];  
  36.         }  
  37.             break;  
  38.         case 3:  
  39.         {  
  40.             NSLog(@"好友申請");  
  41.             [self transitionFromViewController:currentViewController toViewController:thirdViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{  
  42.                  
  43.             }  completion:^(BOOL finished) {  
  44.                 if (finished) {  
  45.                      currentViewController=thirdViewController;  
  46.                 }else{  
  47.                     currentViewController=oldViewController;  
  48.                 }  
  49.             }];  
  50.         }  
  51.             break;  
  52.         default:  
  53.             break;  
  54.     }  

其中我把按鈕設定成不同的tag了。

這時候點擊按鈕,就可以切換子視圖了。

這樣寫的好處: 多個UIViewController之間切換可以添加動畫 當記憶體警告的時候,可以把當前不是啟用狀態的ViewController記憶體釋放。

可以把代碼更好分開 項目原始碼:http://easymorse-iphone.googlecode.com/svn/trunk/changeViewController/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.