1. 程式人生 > >簡單跳轉

簡單跳轉

建立繼承於UIViewController的類 MyViewController
MyViewController.m程式碼`
首先是viewDidLoad

UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(50, 100, 280, 30);
        [btn setTitle:@"第二個介面點選跳回" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(tiao) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];

接下來

-(void)tiao{
    //使用導航控制器 跳轉  pop 和push 左右跳轉 其中引數animated:YES意思是跳轉過程中是否用動畫方式跳轉
//        [self.navigationController popToRootViewControllerAnimated:YES];
    
    //第二種跳轉 是通過ViewController跳轉 Animated:YES也是 是否動畫方式跳轉 present和dismiss 是一對
    [self dismissViewControllerAnimated:YES completion:nil];
}

AppDelegate.m程式碼
匯入標頭檔案#import “ViewController.h”

ViewController * vc = [[ViewController alloc]init];
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:vc];

ViewController.m程式碼
匯入標頭檔案 #import “MyViewController.h”
首先viewDidLoad 裡

UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(50, 100, 280, 30);
    [btn setTitle:@"第一個介面點選跳轉" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(tiao) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];

接下來

-(void)tiao{
    //使用導航控制器 跳轉  pop 和push 左右跳轉 其中引數animated:YES意思是跳轉過程中是否用動畫方式跳轉

    MyViewController * myVc = [[MyViewController alloc]init];
//    [self.navigationController pushViewController:myVc animated:YES];
    //第二種跳轉 是通過ViewController跳轉 Animated:YES也是 是否動畫方式跳轉  present和dismiss 是一對
    [self presentViewController:myVc animated:YES completion:nil];
}