You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

视图控制器间数据传递失效:卡牌猜测游戏传值问题求助

解决卡牌猜测游戏的ViewController传值问题

看起来你已经走对了第一步——在SecondViewController里声明了接收数据的属性,接下来就差把First里用户选择的内容传递过去了。这里给你两种最常用的实现方式,适配不同的跳转场景:


场景1:通过Storyboard Segue跳转(推荐用这种,更贴合iOS规范)

如果你的两个ViewController是通过Storyboard里的Segue(比如点击按钮或选择器触发跳转)切换的,那应该在FirstViewController里重写prepare(for:sender:)方法来传值:

  1. 先在Storyboard里给Segue设置Identifier(比如命名为CardGuessSegue),方便代码识别目标跳转。
  2. 在FirstViewController.m里实现这个方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // 判断是否是我们要处理的Segue
    if ([segue.identifier isEqualToString:@"CardGuessSegue"]) {
        // 获取目标ViewController实例
        SecondViewController *secondVC = segue.destinationViewController;
        
        // 从双组件选择器中获取用户选中的内容
        NSInteger selectedNumberIndex = [self.pickerView selectedRowInComponent:0];
        NSInteger selectedSuitIndex = [self.pickerView selectedRowInComponent:1];
        
        // 假设你有对应的数据源数组(比如numbersArray存卡牌数字,suitsArray存花色)
        NSString *selectedNumber = self.numbersArray[selectedNumberIndex];
        NSString *selectedSuit = self.suitsArray[selectedSuitIndex];
        
        // 给SecondVC的属性赋值,完成传值
        secondVC.number = selectedNumber;
        secondVC.suit = selectedSuit;
    }
}

场景2:手动初始化并跳转(代码触发push/present)

如果是在didSelectRow方法里手动创建SecondViewController实例,直接赋值后再跳转即可:

在FirstViewController的pickerView:didSelectRow:inComponent:方法中写:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // 获取用户选中的卡牌数字和花色
    NSInteger numberIndex = [pickerView selectedRowInComponent:0];
    NSInteger suitIndex = [pickerView selectedRowInComponent:1];
    NSString *chosenNumber = self.numbersArray[numberIndex];
    NSString *chosenSuit = self.suitsArray[suitIndex];
    
    // 初始化SecondViewController
    // 若用xib创建:
    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    // 若用Storyboard创建,先获取Storyboard实例:
    // UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    // SecondViewController *secondVC = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewControllerID"];
    
    // 给属性赋值
    secondVC.number = chosenNumber;
    secondVC.suit = chosenSuit;
    
    // 根据你的导航结构选择跳转方式
    [self.navigationController pushViewController:secondVC animated:YES];
    // 或者用present弹窗:
    // [self presentViewController:secondVC animated:YES completion:nil];
}

额外注意事项

  • 确保SecondViewController.h里的属性是可读写的,比如声明成:
@property (nonatomic, strong) NSString *number;
@property (nonatomic, strong) NSString *suit;

如果你的卡牌数字是整数类型(比如NSInteger),记得把属性类型改成对应类型,赋值时注意类型匹配。

  • 跳转前一定要确认已经正确获取到选择器的选中值,避免出现空值或错误索引。

完成传值后,你在SecondViewController的viewDidLoad或验证逻辑里,就可以通过self.number和self.suit拿到用户的猜测内容,和随机卡牌对比验证啦!

内容的提问来源于stack exchange,提问作者KP.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:59:25