試圖切換控制addChildViewController、_transitionFromViewController,pushviewcontroller

來源:互聯網
上載者:User

試圖切換控制addChildViewController、_transitionFromViewController,pushviewcontroller

試圖切換可以用transitionFromViewController。

步驟:

View Controller中可以添加多個sub view,在需要的時候顯示出來;

可以通過viewController(parent)中可以添加多個child viewController;來控制頁面中的sub view,降低代碼耦合度;

通過切換,可以顯示不同的view;,替代之前的addSubView的管理

代碼:

蘋果API中所帶方法:

addChildViewController: 
removeFromParentViewController 
transitionFromViewController:toViewController:duration:options:animations:completion: 
willMoveToParentViewController: 
didMoveToParentViewController:


下面詳細介紹一下addChildViewController,一個ViewController可以添加多個子ViewController,但是這些子ViewControlle要想顯示出來,需要把子controller的視圖添加到父視圖的相應位置r,可以通過transitionFromViewController:toViewController:duration:options:animations:completion:這個方法轉換顯示的子視圖。同時加入相應的動畫。下面以一個例子來說明這幾個新方法:

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

  1. 建立項目,changeViewController。
  2. 添加相應的viewController,MainViewController、FirstViewController、SecondViewController、ThirdViewController。

3.把MainViewController添加到window中。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    MainViewController *mainViewController=[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; 
    self.window.rootViewController=mainViewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
}

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

5.在MainViewController中添加三個子controller

#pragma mark – View lifecycle

     

- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    
    FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    [self addChildViewController:firstViewController]; 
    
    SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    [self addChildViewController:secondViewController]; 
    
    
    ThirdViewController *thirdViewController=[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; 
    [self addChildViewController:thirdViewController]; 
    
    [contentView addSubview:thirdViewController.view]; 
    
    currentViewController=thirdViewController; 
    
}

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

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

-(IBAction)onClickbutton:(id)sender 

    FirstViewController *firstViewController=[self.childViewControllers objectAtIndex:0]; 
    ThirdViewController *thirdViewController=[self.childViewControllers objectAtIndex:2]; 
    SecondViewController *secondViewController=[self.childViewControllers objectAtIndex:1]; 
    if ((currentViewController==firstViewController&&[sender tag]==1)||(currentViewController==secondViewController&&[sender tag]==2) ||(currentViewController==thirdViewController&&[sender tag]==3) ) { 
        return; 
    } 
    UIViewController *oldViewController=currentViewController; 
    switch ([sender tag]) { 
        case 1: 
        { 
            [self transitionFromViewController:currentViewController toViewController:firstViewController duration:4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ 
            }  completion:^(BOOL finished) { 
                if (finished) { 
                    currentViewController=firstViewController; 
                }else{ 
                    currentViewController=oldViewController; 
                } 
            }]; 

            break; 
        case 2: 
        { 
            [self transitionFromViewController:currentViewController toViewController:secondViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{ 
                
            }  completion:^(BOOL finished) { 
                if (finished) { 
                  currentViewController=secondViewController; 
                }else{ 
                    currentViewController=oldViewController; 
                } 
            }]; 
        } 
            break; 
        case 3: 
        { 
            NSLog(@"好友申請"); 
            [self transitionFromViewController:currentViewController toViewController:thirdViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{ 
                
            }  completion:^(BOOL finished) { 
                if (finished) { 
                     currentViewController=thirdViewController; 
                }else{ 
                    currentViewController=oldViewController; 
                } 
            }]; 
        } 
            break; 
        default: 
            break; 
    } 
}

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

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

可以看到,這些view在沒有使用時,是不會被load的,並且當有Memory Warning時,當前沒有顯示的view自動被unload掉了。

這樣寫的好處:

  • 多個UIViewController之間切換可以添加動畫
  • 當記憶體警告的時候,可以把當前不是啟用狀態的ViewController記憶體釋放。所以新的方法確實能有效地節省記憶體,也能方便地處理記憶體不足時的資源回收
  • 可以把代碼更好分開


iOS開發,怎實現三個View Controller 可以相互切換?

用UINavigationController來組織就可以了,storyboard用起來方便,但不容易理解其本質。
 
ios開發,一個view在幾個viewcontroller切換時位置依然保持在最上面,一直可見

你的view肯定不能塞到你切換的這個navigationcontroller。加到UIWindow裡,或是改變你的視圖結構,實現的辦法有很多。
不知道你說的是否是tabbar
 

聯繫我們

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