Storyboard 里实现页面跳转

本文详细介绍了iOS开发中Storyboard的Segue使用方法,包括push、modal和custom三种跳转方式,并展示了如何通过segue传递参数到目标ViewController。

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

segue有三种跳转方式:push,model,custom
1.点击某个button后根据Identifier跳转
[self  performSegueWithIdentifier:@"pushChatWindow" sender:title];

这里的title可以是id,NSString,NSObject等类型,是页面跳转传递的参数。

如果在这个跳转里需要给目的ViewController传参数,需要在下面里调用[ setValue: forKey:]

forKey:@“key”,这里的key是segue.destinationViewController  的成员变量
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
          //此时这里的sender的值就是上面传过来的title
    [segue.destinationViewController setValue:sender forKey:@"chatterID"];
}


2.如果不想代码实现点击按钮 根据Identifier跳转,传参时需在在ViewController里重写
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
          NSString *name = @“123”;
    [segue.destinationViewController setValue:name forKey:@“name"];
}

3.如果当前ViewController里有多个segue跳转,其中一个 segue跳转后的是一个UINavigationController的根视图,一个是model方式跳转。(没实现[self  performSegueWithIdentifier:@"pushChatWindow"sender:title])
此时需要在当前的ViewController里重写
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"NewColor"]) {//判断是哪个跳转
        //需要传的参数 color
        JZColorDesModel *color = [[JZColorDesModel alloc] init];
        [
self.colors addObject:color];
       
        //通过UIStoryboardSegue对象设置JZColorViewController对象的颜色,给UINavigationController根视图传参
        UINavigationController *nc = (UINavigationController*)segue.destinationViewController;
       
 ColorViewController *mvc = (ColorViewController *)[nc topViewController];
        mvc.
colorDes = color;
//    也可以用 [mvc setValue:color forKey:@"colorDes"]          
    }else if([segue.identifier isEqualToString:@"ExitingColor"]){
//点击UITableViewCell    push下一个ViewController
        //对于push样式的UIStoryboardSegue对象,senderUITableViewCell对象
       
 NSIndexPath *ip = [self.tableView indexPathForCell:sender];
       
       
 JZColorDesModel *color = self.colors[ip.row];
       
 //设置ColorViewController对象的颜色
       
 //同时设置其exitingColor属性为YES
       
 ColorViewController *cvc = (ColorViewController *)segue.destinationViewController;
        cvc.colorDes = color;
        cvc.existingColor = YES
 //    也可以用 [cvc setValue:color forKey:@"colorDes"]          
       
 //colorColorViewController里的成员变量colorDes指向同一块内存。
    }
}



//根据 segue Identifier跳转界面

    [self performSegueWithIdentifier:@"GotoTwo" sender:self];

    

   //modal 方式跳转
    [self presentModalViewController:VC animated:YES];

    

   //压进一个viewcontroller
    [self.navigationController pushViewController:VC animated:YES];


   //弹出一个viewcontroller  相当与返回上一个界面

    [self.navigationController popViewControllerAnimated:YES];

    

   //  modal跳转的返回方法

    [self dismissModalViewControllerAnimated:YES];





4.storyboard里不用拖线方式来push视图

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *VC = [sb instantiateViewControllerWithIdentifier:@“second"];
    [self.navigationController pushViewController:VC animated:YES];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值