3

I am new to Objective C coming from C# .NET, I have this scenario :

Assume I have 5 NSArrays corresponding to 5 UIButtons. the UIButtons have the exact same name as the NSArray, so for example one UIButton is called mainScreen, and there is an NSArray called mainScreen.

Those Five buttons are linked to one IBAction where I do the following :

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSLog(category);
   //Here I need to call the NSArray which has the same name as category
}

Now I can get the actual name of the UIButton, but how can I get the NSArray same as that title? without getting into a lot of if else or switch statements?

Sebastian
  • 7,670
  • 5
  • 38
  • 50
funkycoldmedia
  • 131
  • 4
  • 16

5 Answers5

1

The easiest way to associate names with objects is using an NSDictionary (or it's mutable subclass NSMutableDictionary). Dictionaries map a unique key to an object. Keys can be any object (that implements the NSCopying protocol), but are very often NSStrings

Have a look at the NSDictionary Reference and the Programming with Objective-C guide.

Note that if you use the button title this might break if you localise your app.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
1

What I would do is store the arrays inside an NSDictionary. You can then set the 'key' as the name of your array and then the value would be the array itself.

That way you could say:

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSLog(category);
    //Here I need to call the NSArray which has the same name as category
    NSArray *theArray = (NSArray*)[self.myDictionary valueForKey:category];
}

Hope this helps!

LuisCien
  • 6,362
  • 4
  • 34
  • 42
1

What you do is not the best way. You should provide tag for each button, say from 1 to 5. Also you should put your five arrays into one array. Now all you need is:

- (IBAction)btnClick:(id)sender
{
  NSInteger index = [sender tag] - 1;

  NSArray *array = [bigArray objectAtIndex:index];
} 

That's it.

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
0

Assign different tags to all UIButtons and then access them explicitly using their tags.

- (IBAction)btnClick:(id)sender {

    int tagIs  = [(UIButton *)sender tag];
    switch (tagIs) {
        case 1:
            // Access first button array
            break;
        case 2:
            // Access second button array

            break;
        default:
            break;
    }
}

Or you can use AssociationObjects method for associating data with objects as following: Firstly import :

#import <objc/runtime.h>

then create keys as :

static char * firstBtnKey = "firstBtnKey";
static char * secondBtnKey = "secondBtnKey";
  • -- - other keys same way ---

then use :

// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects:@"object1",@"object 2", nil];
objc_setAssociatedObject((UIButton *)[self.view viewWithTag:1],
                         firstBtnKey,
                         firstArray,
                         OBJC_ASSOCIATION_RETAIN);

NSMutableArray *secondArray = [[NSMutableArray alloc] initWithObjects:@"object1",@"object 2", nil];


objc_setAssociatedObject((UIButton *)[self.view viewWithTag:2],
                         secondBtnKey,
                         secondArray,
                         OBJC_ASSOCIATION_RETAIN);

`

and then access these arrays as :

- (IBAction)btnClick:(id)sender {

    int tagIs  = [(UIButton *)sender tag];
    switch (tagIs) {
        case 1:
            // Access first button array
    NSMutableArray *tempArr = (NSMutableArray *)objc_getAssociatedObject((UIButton *)sender, firstBtnKey);

            break;
        case 2:
            // Access second button array
    NSMutableArray *tempArr = (NSMutableArray *)objc_getAssociatedObject((UIButton *)sender, secondBtnKey);

            break;
        default:
            break;
    }
}

Hope it helps.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
0

In most programming languages objects don't have names.[1] Just because UIButtons have the exact same name as the NSArray(mainScreen), doesn't mean that your object is "called" mainScreen.
Use NSDictionary , array as object and button title as key. or use button tag

title1= [[NSArray alloc] initWithObjects:@"1",nil];
title2= [[NSArray alloc] initWithObjects:@"2",nil];
title3= [[NSArray alloc] initWithObjects:@"3",nil];
title4= [[NSArray alloc] initWithObjects:@"4",nil];
title5= [[NSArray alloc] initWithObjects:@"5",nil];

dict = [[NSDictionary alloc] initWithObjectsAndKeys:title1,@"title1",title2,@"title2",title3,@"title3",title4,@"title4",title5,@"title5",nil];  

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSArray *arr = [dict objectForKey:category];

}
Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144