I want to show different set of strings with different calculation depends on the sender's response..2 sets of strings are parallel, which means the naming is like.. user1, user2, object1, object2, name1, name2, age1, age2, etc..what I know so far is to use...
-(IBAction)showLotOfStuff:(id)sender{
switch ([sender tag]) {
case 1: //show user1, object1, name1, age1...
case 2: //show user2, object2, name2 age2...}
However, by then I will have double size and repetitive codes as I have a lot to do and show..it's really dumb, it involves array, calculation and I really dun want to see it twice, though I can do it by simply copy and paste..
I wonder if there is any way I can cut half of them by doing..
-(IBAction)showLotOfStuff:(id)sender{
show user[sender tag], object[sender tag],
name[sender tag], age[sender tag]...}
this question is quite basic I think, but I just can't figure out how to do it.. Thx for your help!
Do you have an array of users? What does your data look like? You can probably refactor this and not duplicate code.
Let's say you have an NSArray
called users
.
Not sure what kind of controls you are using, but for simplicity's sake we can assume you have some buttons, each of which should display the info for one of the users. You can give the buttons tags corresponding to the position of the user of the array, ideally with some offset to reduce the chance of making a mistake an using those tags for something else. (ie let's assume you have 3 buttons with tags 100, 101, and 102, and 3 users in your array).
Then you can just do something like:
- (IBAction)showLotOfStuff:(id)sender
{
int userIndex = sender.tag - 100; // you could define 100 as a constant called userOffset or something to make this a little cleaner
if(users.count <= userIndex)
return; // out of bounds, probably want to check for this and handle it somehow
MyObjectType *currentUser = [users objectAtIndex:userIndex];
// do whatever with your user
}
This is a general example. The specific implementation depends on more details you haven't provided.