iOS開發——界面跳轉方法總結

一、UITableBarController(標籤欄控制器)中的界面跳轉

我是在AppDelegate.m中寫的代碼,代碼如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    //創建標籤欄控制器tabBarController
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    //初始化firstView和secondView
    FirstViewController *firstView = [[FirstViewController alloc] init];
    SecondViewController *secondView = [[SecondViewController alloc]init];

    //添加子視圖控制器firstView和secondView
    [tabBarController addChildViewController:firstView];
    [tabBarController addChildViewController:secondView];

    //設置底部標籤欄上的title
    firstView.tabBarItem.title = @"first";
    secondView.tabBarItem.title = @"second";

    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

效果如下:
這裏寫圖片描述

二、UINavigationController(導航控制器)中的界面跳轉

AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    FirstViewController *firstView = [[FirstViewController alloc] init];

    //創建導航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstView];
    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];
    return YES;
}

FirstViewController.m中

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    //創建一個Lable標記這是fistView
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    lable.text = @"fist";
    [self.view addSubview:lable];

    //創建一個button實現界面間的跳轉
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(50, 150, 150, 100);

    [button setTitle:@"To secondView" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];
}

-(void)buttonPressed
{
    SecondViewController *secondView = [[SecondViewController alloc] init];

    //跳轉到secondView的實現方法
    [self.navigationController pushViewController:secondView animated:YES];
}

這裏寫圖片描述這裏寫圖片描述
UINavigationController中跳轉有三個方法,上述例子只實現了一個方法,還有其他兩個方法。這裏總結一下:

1.[self.navigationController pushViewController:firstView animated:YES];跳轉到firstView界面
2.[self.navigationController popViewControllerAnimated:YES]; //返回上一頁面
3.[self .navigationController popToRootViewControllerAnimated: YES ];  //返回根控制器

三、模態視圖
很簡單,就兩個方法

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

[ self dismissViewControllerAnimated: YES completion: nil ];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章