切换视图 UIViewController

RootViewController.m代码:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor yellowColor];

    //添加切换视图按钮

    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100, 50)];

    [btn setTitle:@"切换视图" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(didBtn:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

-(void)didBtn:(UIButton *)sender{

    OtherViewController *other=[[OtherViewController alloc]init];

    other.name=@"张三";//属性传值

    //[other setModalTransitionStyle:UIModalTransitionStyleCoverVertical];//默认从下面拉入

    //[other setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];//溶解

    //[other setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];//水平翻转

    [other setModalTransitionStyle:UIModalTransitionStylePartialCurl];//翻页

    //切换视图

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

}


OtherViewController.m代码:

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];

    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100, 50)];

    [btn setTitle:@"关闭视图" forState:UIControlStateNormal];

    btn.backgroundColor=[UIColor blueColor];

    [btn addTarget:self action:@selector(closeBtn:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    NSLog(@"name=%@",self.name);  //属性值

}

-(void)closeBtn:(UIButton *)sender{

    //关闭视图

    [self dismissViewControllerAnimated:YES completion:nil];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 中,要实现点击单元格切换视图的功能,有以下几个步骤: 1. 创建一个 UITableView,并设置其代理和数据源。 2. 在代理方法 `tableView(_:didSelectRowAt:)` 中,监听单元格的点击事件。 3. 在点击事件中,获取点击的单元格索引,然后根据需要进行视图切换。 下面是一个简单的示例代码: ```swift import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // 设置tableView的frame和代理 tableView.frame = view.bounds tableView.delegate = self tableView.dataSource = self // 注册单元格 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") // 添加tableView到视图中 view.addSubview(tableView) } // 返回单元格数量 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } // 创建和配置单元格 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Cell \(indexPath.row + 1)" return cell } // 单元格点击事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 获取点击的单元格索引 let selectedRow = indexPath.row // 根据需要进行视图切换 switch selectedRow { case 0: // 切换视图1 let viewController1 = ViewController1() navigationController?.pushViewController(viewController1, animated: true) case 1: // 切换视图2 let viewController2 = ViewController2() navigationController?.pushViewController(viewController2, animated: true) // 其他切换逻辑... default: break } // 取消选中效果 tableView.deselectRow(at: indexPath, animated: true) } } ``` 在上述示例中,我们创建了一个包含 5 个单元格的 UITableView,并通过点击单元格来切换到不同的视图。你可以根据自己的需求,定义不同的视图,并在 `didSelectRowAt` 方法中进行切换逻辑的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值