Storyboard 里实现页面跳转


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 对象, sender UITableViewCell 对象
       
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"]           
       
 //color ColorViewController 里的成员变量 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];

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值