zoukankan      html  css  js  c++  java
  • StoryBoard中使用segue传值

    需求描述:

    故事板(StoryBoard)中,ViewController1与ViewController2有一条segue连线。点击ViewController1中的按钮跳转至ViewController2,并且从ViewController1中传递值给ViewController2。

    实现:

    ViewController1.m

    在点击按钮时进行视图跳转。

    _res = @"YES";
    [self performSegueWithIdentifier:@"gameover" sender:sender];

    添加下面的事件方法,该方法在视图跳转时被触发。

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
       if([segue.identifier isEqualToString:@"gameover"]) //"gameover"是segue连线的标识
        {
           id theSegue = segue.destinationViewController;
          [theSegue setValue:_res forKey:@"result"];
        }
    }

    @"gameover"是segue的identifier,_res是要传的值,为NSString类型,传递的形参取名为result。

    ViewController2.h

    定义一个属性来接受segue传递过来的值:

    @property(nonatomic,weak)NSString *result;

    ViewController2.m

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"接收到的值为: %@",self.result);
        if ([self.result isEqualToString:@"YES"]) {
            self.res.text = @"你赢了";
            self.res.textColor = [UIColor greenColor];
        }
        // Do any additional setup after loading the view.
    }

    可以输出传递的值,若传的值为@"YES",则输出"你赢了"。

  • 相关阅读:
    ACM Dance Recital(dfs+剪枝)
    矩阵快速幂
    分页实现复选框的选中状态
    MemCached 和redis的区别
    调用存储过程传值
    实现js中的时间格式中的T
    实现下载完成
    模态框实现功能后刷新父类页面
    JSON.parse()、JSON.stringify()和eval()的作用
    全选反选珍藏班版
  • 原文地址:https://www.cnblogs.com/luoyihao/p/12831552.html
Copyright © 2011-2022 走看看