Search code examples
iosarraysbuttonpushviewcontrollersender

Weird situation trying to pushView with an object


So, here's the deal. I have 16 buttons in my xib, each is instanced in its controller with the following code:

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;

When one of those is pressed, it should push a view with an object depending on which button was pressed. Here's how I did that:

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];

Now the problem is I can't figure out why if I press left1, left2 or left3 it works perfectly. Now if I press left4 it crashes, sometimes with an exception (NSCFArray number unrecognized selector sent blalbal) and sometimes without it. All teeth with number 11, 12, 13 and 14 exist in an array. I only tried those since I was too lazy to create 36 of them. But how come it doesnt work only for the number 14?

I've been trying to figure this out for hours unsuccessfully. Any help apreciated.

UPDATE: (The last update was wrong) I deleted the teeth number 14 from the xml from which the teeth are parsed from and added a tooth number 15. The problem persists with teeth number 15 and show the correct message (tooth doesnt exist) for tooth number 14.

UPDATE 2: I added a "aTooth = nil" after the [aTooth release] and now I can access teeth 15, but I can't access any teeth twice. Once I accessed a tooth, I cant go back there withouth re-compiling.


Solution

  • [aTooth release] - ??? So you're releasing object after retrieving it from a dictionary?