简单跳转

本文详细介绍了在iOS应用中如何创建继承于UIViewController的类,并实现界面间的跳转。包括使用导航控制器进行push和pop操作,以及通过present和dismiss实现模态视图控制器的显示和隐藏。文中提供了具体的代码示例,帮助开发者理解并掌握iOS界面跳转的基本原理和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建继承于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];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值