Outlets do not work after showing & ldquo; old & rdquo; meet again

advertisements

I have several UIViews in my Storyboard and, of course, I can switch between them using a segue. Initially this works just fine: notenKurse is a NSMutableArray, and kurse1Outlets is an outlet collection with my UITextFields.

int counter = 0;
for (UITextField *tf in kurse1Outlets) {
    NSMutableString *t = [NSMutableString stringWithFormat:@"%@", [notenKurse objectAtIndex:counter]];
    NSLog(@"Object at index %i is %@", counter, [notenKurse objectAtIndex:counter]);
    if ([t isEqualToString:@"42"]) {
        [t setString:@""];
    }
    [tf setText:t];
    NSLog(@"UITextField in slot %i should now display %@", counter, t);
    counter++;
}

All of my UITextFields are displaying the value stored in the array. But if I go to another view (let's assume I have a Button for it ;) ) Change something, and then go back to the original UIView the above code still gets executed, and there are different values in the array (this is supposed to be). I can see that in the log. But the stupid UITextField just doesn't display anything. Neither what was in there before, nor the new text. But why? The log clearly shows that t is what it's supposed to be, so the error must be in writing it into the textfield, and therefore I guess it's an outlet issue...


There is no guarantee of the order of your outlet collection. It's treated very much like an NSDictionary as opposed to an NSArray - where order is guaranteed. Iterating over this sort of collection will yield different results for different devices/people/phase of the moon.

When I use a collection like this I tend to set the 'tag' and then reorder the outlet collection when viewDidLoad by sorting off of the tag.

self.calendarDayImageViews = [_calendarDayImageViews sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    if ([(UIView *)obj1 tag] < [(UIView *)obj2 tag]) {
        return NSOrderedAscending;
    }
    else if([(UIView *)obj1 tag] > [(UIView *)obj2 tag]){
        return NSOrderedDescending;
    }
    else{
        return NSOrderedSame;
    }
}];