ios - 尝试用对象推送View 的奇怪情况

标签 ios arrays button pushviewcontroller sender

所以,事情就是这样。 我的 xib 中有 16 个按钮,每个按钮都在其 Controller 中使用以下代码进行实例化:

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;

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

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 数字无法识别的选择器发送了 blabal),有时则没有异常。所有编号为 11、12、13 和 14 的 dentry 都存在于一个阵列中。我只尝试了这些,因为我懒得创建其中的 36 个。但为什么它只适用于数字 14?

几个小时以来我一直试图解决这个问题,但没有成功。 任何帮助表示感谢。

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

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

最佳答案

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

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

关于ios - 尝试用对象推送View 的奇怪情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8422472/

相关文章:

java - 执行被困在按钮内

ios - 编译 QtQuick2 应用程序时出现重复符号

iphone - iPhone的Twitter API?

javascript - 为什么在数组上使用一元运算符 + 在 javascript 中会产生不一致的结果?

java - 使用静态字典作为缓存可能会导致泄漏问题?

java - setOnClickListener 从未触发 - Android/Eclipse

ios - RestKit: 'Unacceptable type of value for to-one relationship: property = " Prop ”;所需类型 = A 型;给定类型 = TypeS;

ios - iOS 和 Android 设备之间通过 wifi 的消息传递应用程序

javascript - 用Js实现有趣的游戏

css - 带有指向上次查看页面的链接的 float 按钮