【问题标题】:Weird situation trying to pushView with an object尝试使用对象 pushView 的奇怪情况
【发布时间】:2011-12-07 21:02:30
【问题描述】:

所以,这就是交易。 我的 xib 中有 16 个按钮,每个按钮都在其控制器中使用以下代码进行实例化:

IBOutlet UIButton *left1;
IBOutlet UIButton *left2;
IBOutlet UIButton *left3;
IBOutlet UIButton *left4;
IBOutlet UIButton *left5;
IBOutlet UIButton *left6;
IBOutlet UIButton *left7;
IBOutlet UIButton *left8;
IBOutlet UIButton *right1;
IBOutlet UIButton *right2;
IBOutlet UIButton *right3;
IBOutlet UIButton *right4;
IBOutlet UIButton *right5;
IBOutlet UIButton *right6;
IBOutlet UIButton *right7;
IBOutlet UIButton *right8;

当其中一个被按下时,它应该根据按下的按钮推送带有对象的视图。我是这样做的:

if (upperLower.selectedSegmentIndex == 0) {
    if (sender == left1) {
        aTooth = [aPatient.teeth objectForKey:@"11"];
    } else if (sender == left2) {
        aTooth = [aPatient.teeth objectForKey:@"12"];
    } else if (sender == left3) {
        aTooth = [aPatient.teeth objectForKey:@"13"];
    } else if (sender == left4) {
        aTooth = [aPatient.teeth objectForKey:@"14"];
    } else if (sender == left5) {
        aTooth = [aPatient.teeth objectForKey:@"15"];
    } else if (sender == left6) {
        aTooth = [aPatient.teeth objectForKey:@"16"];
    } else if (sender == left7) {
        aTooth = [aPatient.teeth objectForKey:@"17"];
    } else if (sender == left8) {
        aTooth = [aPatient.teeth objectForKey:@"18"];
    } else if (sender == right1) {
        aTooth = [aPatient.teeth objectForKey:@"21"];
    } else if (sender == right2) {
        aTooth = [aPatient.teeth objectForKey:@"22"];
    } else if (sender == right3) {
        aTooth = [aPatient.teeth objectForKey:@"23"];
    } else if (sender == right4) {
        aTooth = [aPatient.teeth objectForKey:@"24"];
    } else if (sender == right5) {
        aTooth = [aPatient.teeth objectForKey:@"25"];
    } else if (sender == right6) {
        aTooth = [aPatient.teeth objectForKey:@"26"];
    } else if (sender == right7) {
        aTooth = [aPatient.teeth objectForKey:@"27"];
    } else if (sender == right8) {
        aTooth = [aPatient.teeth objectForKey:@"28"];
    }
} 
else {
    if (sender == left1) {
        aTooth = [aPatient.teeth objectForKey:@"41"];
    } else if (sender == left2) {
            aTooth = [aPatient.teeth objectForKey:@"42"];
    } else if (sender == left3) {
            aTooth = [aPatient.teeth objectForKey:@"43"];
    } else if (sender == left4) {
            aTooth = [aPatient.teeth objectForKey:@"44"];
    } else  if (sender == left5) {
            aTooth = [aPatient.teeth objectForKey:@"45"];
    } else  if (sender == left6) {
            aTooth = [aPatient.teeth objectForKey:@"46"];
    } else  if (sender == left7) {
            aTooth = [aPatient.teeth objectForKey:@"47"];
    } else  if (sender == left8) {
            aTooth = [aPatient.teeth objectForKey:@"48"];
    } else  if (sender == right1) {
            aTooth = [aPatient.teeth objectForKey:@"31"];
    } else  if (sender == right2) {
            aTooth = [aPatient.teeth objectForKey:@"32"];
    } else  if (sender == right3) {
            aTooth = [aPatient.teeth objectForKey:@"33"];
    } else  if (sender == right4) {
            aTooth = [aPatient.teeth objectForKey:@"34"];
    } else  if (sender == right5) {
            aTooth = [aPatient.teeth objectForKey:@"35"];
    } else  if (sender == right6) {
            aTooth = [aPatient.teeth objectForKey:@"36"];
    } else  if (sender == right7) {
            aTooth = [aPatient.teeth objectForKey:@"37"];
    } else  if (sender == right8) {
            aTooth = [aPatient.teeth objectForKey:@"38"];
    }
}
toothController.aTooth = aTooth;
toothController.aPatient = aPatient;

if (aTooth) {
    [self.navigationController pushViewController:toothController animated:YES];    
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tooth not found" message:@"The patient doesn't have that tooth" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
[aTooth release];

现在的问题是我无法弄清楚为什么如果我按 left1、left2 或 left3 会完美运行。现在,如果我按 left4,它会崩溃,有时会出现异常(NSCFArray number unrecognized selector sent blalbal),有时会出现异常。所有编号为 11、12、13 和 14 的牙齿都存在于一个阵列中。我只尝试了这些,因为我懒得创建 36 个。但是为什么它只对数字 14 不起作用?

我已经尝试了几个小时没有成功。 任何帮助表示赞赏。

更新:(上次更新错误)我从解析牙齿的 xml 中删除了 14 号牙齿,并添加了 15 号牙齿。问题仍然存在于 15 号牙齿并显示正确的消息(牙齿没有存在)对于 14 号牙齿。

更新 2:我在 [aTooth 版本] 之后添加了一个“aTooth = nil”,现在我可以访问 15 颗牙齿,但我无法访问任何牙齿两次。一旦我访问了一颗牙齿,我就无法在不重新编译的情况下回到那里。

【问题讨论】:

  • 如何填充 aPatient.teeth?
  • aPatient.teeth 是一个 NSMutableDictionary,该字典的每个对象都是一个牙齿,它有自己的类(Tooth)
  • 哦,每颗牙齿和患者等等都是从 xml 中获取的。 14 号牙齿的结构与其他牙齿完全相同

标签: ios arrays button pushviewcontroller sender


【解决方案1】:

objectForKey: 是一个NSDictionary 方法。您收到一个异常,表明teethNSArray。我相信你想使用

aTooth = [aPatient.teeth objectAtIndex:some-index];

【讨论】:

  • 其实牙齿是一个NSMutableDictionary
  • 只有在我清理所有目标后才会显示异常,如果我在清理所有目标后再次编译它不会显示异常,只会崩溃
  • 您确认teeth 引用了一个有效的NSMutableDictionary,但尚未自动释放?
  • 牙齿未设置为自动释放,但即使设置为自动释放,为什么它适用于 11、12 和 13 号牙齿而不是 14 号? 14 与其他人在同一个 NSMutableDictionary 中:S
  • 好问题。不用在调试器下观察,就根据你的描述在黑暗中拍摄。
【解决方案2】:

[aTooth release] - ???所以你在从字典中检索对象后释放它?

【讨论】:

  • 我正在初始化它,设置它的值,将牙齿添加到牙齿 NSMutableArray,释放一个牙齿,然后重新开始新牙齿
  • 没有。在您的代码中,您有 aTooth = [aPatient.teeth objectForKey: ...]; toothController.aTooth = aTooth; ... [aTooth release]; 所以 aTooth 将在首次使用后被释放,并且数组将包含无效对象。
猜你喜欢
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
相关资源
最近更新 更多