Search code examples
xcodexcode4xcode4.5

XCode renaming Outlet label direction


From a string I created (Using xcode 4.5.2)

   - (void)viewDidLoad{ 
      myList = [[NSArray alloc] initWithObjects:@"x",@"y",@"z", nil];
      }

and also of course:

@property (strong, nonatomic) IBOutlet UILabel *labelField_x;
@property (strong, nonatomic) IBOutlet UILabel *labelField_y;
@property (strong, nonatomic) IBOutlet UILabel *labelField_z;

I want to be able, inside an IBAction, to choose from the string what Label to use by just picking the correct key from the array. Instead of doing if or BOOL statements and repeating my self. Form example I have this:

- (IBAction)buttonPressed:(UIButton *)sender;
{
    if (is_x){

        NSString *digit = sender.currentTitle;
            if (!pressedDecimal){
                labelField_x.text = [labelField_x.text stringByAppendingString:digit];
            }else if (count <= 1){
                labelField_x.text = [labelField_x.text stringByAppendingString:digit];
            }
                                      .
                                      .
                                      .
    }
    else if (is_y){

        NSString *digit = sender.currentTitle;     
            if (!pressedDecimal){
                labelField_y.text = [labelField_y.text stringByAppendingString:digit];
            }else if (count <= 1){
                labelField_y.text = [labelField_y.text stringByAppendingString:digit];
            }
                                      .
                                      .
                                      .

    }
    else if (is_z){

        NSString *digit = sender.currentTitle;
            if (!pressedDecimal){
                labelField_z.text = [labelField_z.text stringByAppendingString:digit];
            }else if (count <= 1){
                labelField_z.text = [labelField_z.text stringByAppendingString:digit];
            }
                                      .
                                      .
                                      .
    }
}

As you can see I repeat my self every time with the same code instead only changing the location or the UILabel direction or whatever it is pointing at label ( labelField_x, labelField_y, labelField_z ). My question is if there is a way to change only the last string (x, y, z) and call it from a set of array that I created and tell which label to use.

I have tried this but of course it doesn't work and is nothing close to the correct answer but hope this can explain better what I want (sorry in advance for bad coding):

labelField_@"%@".text,[myList 0]  = [labelField_@"%@".text,[myList 0] stringByAppendingString:digit];

Thank you.


Solution

  • I'm not entirely sure what you're up to, but if you want to save yourself some typing, why don't you do something like this:

    // assign the label you want to use to a local variable
    
    UILabel *selectedLabel;
    selectedLabel = nil;
    if (is_x) {
        selectedLabel = labelField_x;
    } else if (is_y) {
        selectedLabel = labelField_y;
    } else if (is_z) {
        selectedLabel = labelField_z;
    }
    
    // now work with selectedLabel so you don't have to repeat yourself
    
    if (!pressedDecimal){
        selectedLabel.text = ...
    

    Or you could even stick the labels from your IBOutlet properties right into a dictionary so you can quickly get the right label for any given character (string):

    // create a lookup dictionary (once, e.g. in awakeFromNib)
    
    NSDictionary *labelForCharacter;
    labelForCharacter = @{
        @"x" : labelField_x,
        @"y" : labelField_y,
        @"z" : labelField_z
    };
    
     // get the label for "z"
    
    UILabel *selectedLabel;
    selectedLabel = labelForCharacter[@"z"];