IOS StroyBoard 切換操作

來源:互聯網
上載者:User

標籤:style   color   io   os   ar   for   sp   on   c   

storyboard之間的切換有三種方法:

[self presentViewController:viewControllerToPresent animated:YES completion:nil];

[self.navigationController pushViewController:viewControllerToPresent animated:YES];

[self.navigationController setViewControllers:ViewControllers animated:YES];

前兩種方法都是在當前VC的基礎上進行PUSH操作,

第三種方法可以自己排列當前的VC棧,一個典型的案例是:當前登入狀態下,退出登入,需要重新返回到登入介面,這個時候當前登入介面就成為根介面(所有的介面都是退不回去),用setViewControllers。

 

程式設計為這樣:

    1,建立Empty工程

    2,建立故事板FirstStoryBoard,先後拖進Navigation Controller,Tab Bar Controller(自動產生2個普通的View Controller),再拖進一個View Controller,成為Tab Bar Controller的第三個Tab。

        Navigation Controller和Tab Bar Controller用root view進行segue,Tab Bar Controller和View Controller之間用view controllers進行segue。

        更改Tab Bar Controller的identity屬性面板中的Storyboard id為TABBAR。

        更改View Controller的Tab Bar Item的名稱為Tab1,Tab2,Tab3,之後Tab Bar Controller的顯示名稱也同時變成Tab1,Tab2,Tab3。

        在Tab2所對應的第二個View Controller添加一個按鈕,按鈕文本為:這是FirstStoryBoard,點擊切換到SecondStoryBoard。

        嚮導添加ViewControllerFirstTab2類,繼承UIViewController,增加按鈕的action動作,後面添加代碼。

    3,建立故事板SecondStoryBoard,拖進Navigation Controller,View Controller,用root view進行segue。

        更改View Controller的identity屬性面板中的Storyboard id為SenondViewController。

        在View Controller添加第一個按鈕,按鈕文本為:這是SecondStoryBoard,點擊切換到SecondStoryBoard。

        在View Controller添加第二個按鈕,按鈕文本為:這是SecondStoryBoard,點擊切換到FirstStoryBoard。

        嚮導添加ViewControllerSecond類,增加按鈕的action動作,後面添加代碼。

    4,建立故事板ThirdStoryBoard,拖進Navigation Controller,View Controller,用root view進行segue。

        更改View Controller的Show the identity inspector屬性面板中的Storyboard id為ThreeViewController。

        在View Controller添加第一個按鈕,按鈕文本為:這是SecondStoryBoard,點擊切換到SecondStoryBoard。

        在View Controller添加第二個按鈕,按鈕文本為:這是SecondStoryBoard,點擊切換到FirstStoryBoard。

        嚮導添加ViewControllerThird類,增加按鈕的action動作,後面添加代碼。

 

下面開始添加代碼:

1,

AppDelegate.m代碼:

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

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    UIStoryboard *StoryBoard = [UIStoryboard storyboardWithName:@"FirstStoryBoard" bundle:nil];

    self.window.rootViewController=[StoryBoard instantiateInitialViewController];

    return YES;

}

 

 2,

ViewControllerFirstTab2.h代碼:

@interface ZViewControllerFirstTab2 : UIViewController

- (IBAction)buttonActionSecond:(id)sender;

@end

 

ViewControllerFirstTab2.m代碼:

- (IBAction)buttonActionSecond:(id)sender {

    NSString *NSStringStoryBoard = @"SecondStoryBoard";

    NSString *NSStringVC = @"Second";

    

    UIStoryboard *Storyboard = [UIStoryboard storyboardWithName:NSStringStoryBoard bundle:nil];

    if(nil == Storyboard) NSLog(@"NO %@", NSStringStoryBoard);

    

    UIViewController *VC = [Storyboard instantiateViewControllerWithIdentifier:NSStringVC];

    if(nil == VC) NSLog(@"NO %@", NSStringVC);

    

    /*

     /// 該方法直接操作VC,不需要NAV,與presentViewController對應的函數是dismissViewControllerAnimated

     /// 該函數已經廢棄

     /// [self presentModalViewController:[Storyboard instantiateInitialViewController] animated:(TRUE)];

     [self presentViewController:VC animated:YES completion:nil];

     */

    

    if(nil == self.navigationController) NSLog(@"null navigationController");

    else

    {

        self.navigationController.navigationBarHidden = YES;

        [self.navigationController pushViewController:VC animated:YES];

    }

}

 

3,

ViewControllerSecond.h代碼:

@interface ZViewControllerSecond : UIViewController

- (IBAction)buttonActionThree:(id)sender;

- (IBAction)buttonActionOne:(id)sender;

@end

 

ViewControllerSecond.m代碼:

- (IBAction)buttonActionThree:(id)sender {

    NSString *NSStringStoryBoard = @"ThirdStoryBoard";

    NSString *NSStringVC = @"Third";

    

    UIStoryboard *Storyboard = [UIStoryboard storyboardWithName:NSStringStoryBoard bundle:nil];

    if(nil == Storyboard) NSLog(@"NO %@", NSStringStoryBoard);

    

    UIViewController *VC = [Storyboard instantiateViewControllerWithIdentifier:NSStringVC];

    if(nil == VC) NSLog(@"NO %@", NSStringVC);

    

    /*

    /// 該方法直接操作VC,不需要NAV,與presentViewController對應的函數是dismissViewControllerAnimated

    /// 該函數已經廢棄

    /// [self presentModalViewController:[Storyboard instantiateInitialViewController] animated:(TRUE)];

    [self presentViewController:VC animated:YES completion:nil];

    */

    

    if(nil == self.navigationController) NSLog(@"null navigationController");

    else

    {

        self.navigationController.navigationBarHidden = YES;

        [self.navigationController pushViewController:VC animated:YES];

    }

}

 

- (IBAction)buttonActionOne:(id)sender {

    /// 返回第一個介面時,第一個介面保留切換時的狀態

    /// [self dismissViewControllerAnimated:YES completion:nil];

    [self.navigationController popViewControllerAnimated:YES];

}

 

4,

ViewControllerThird.h代碼:

@interface ZViewControllerThird : UIViewController

- (IBAction)buttonActionSecond:(id)sender;

- (IBAction)buttonActionFirst:(id)sender;

@end

 

ViewControllerThird.m代碼:

- (IBAction)buttonActionSecond:(id)sender

{

    /// [self dismissViewControllerAnimated:YES completion:nil];

    [self.navigationController popViewControllerAnimated:YES];

}

 

- (IBAction)buttonActionFirst:(id)sender

{

    NSString *NSStringStoryBoard = @"FirstStoryBoard";

    NSString *NSStringVC = @"TABBAR";

    

    UIStoryboard *Storyboard = [UIStoryboard storyboardWithName:NSStringStoryBoard bundle:nil];

    UIViewController *VC = [Storyboard instantiateViewControllerWithIdentifier:NSStringVC];

    

    if(nil == self.navigationController) NSLog(@"null navigationController");

    NSMutableArray *ViewControllers = [self.navigationController.viewControllers mutableCopy];

    [ViewControllers removeAllObjects];

    [ViewControllers addObject:VC];

    NSLog(@"%d", [ViewControllers count]);

    [self.navigationController setViewControllers:ViewControllers animated:YES];

}

IOS StroyBoard 切換操作

聯繫我們

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