0

I've declared an enum like so:

typedef enum
{
    firstView = 1,
    secondView,
    thirdView,
    fourthView
}myViews

My goal is that a UIButton will trigger another function with the uiButton sender.tagand that function will know to convert the integer to the correct view. I'm aware that I can just create an array with the names of the views but I am looking for something smarter than that using the declared enum.

Example:

-(void)function:(UIButton *)sender
{
  ...
  ...
  NSLog(@"current View: %@",**converted view name from sender.tag);
}

Thanks

Segev
  • 19,035
  • 12
  • 80
  • 152

3 Answers3

3

Well, the best solution is to actually store the views. You can also use a IBOutletCollection to create the array. Declaring an enum is just another way to store names.

self.views = @[firstView, secondView, thirdView, forthView];

...

button.tag = [self.views indexOfObject:firstView];

...

- (void)buttonTappedEvent:(UIButton*)sender {
    UIView* view = [self.views objectAtIndex:sender.tag];
}

PS: converting tag into enum is trivial, it's just myViews viewName = sender.tag, possibly with a cast myViews viewName = (myViews) sender.tag

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • I just saw that you gave the same solution I ended up doing. One question, `myViews viewName = (myViews) sender.tag; NSLog(@"View #: %u",viewName);` will output a number and not the enum name – Segev May 27 '13 at 07:12
  • @EXEC_BAD_ACCESS True. An enum is just a symbolic name. If you write `int a = 1; NSLog(@"%d", a)`, you don't expect it to output `a`. If you want to log a descriptive name, you have to convert the enum value to a string. There are different ways how to do it - e.g. declaring a second array with names or X-macros. In most situations the integer number is enough because you know what it means. – Sulthan May 27 '13 at 09:23
0

How about storing it inside a NSMutableDictionary ?

NSMutableDictionary *viewList = [[NSMutableDictionary alloc] init];

for(int i = 1; i <= 4; i++)
{
    [viewList setObject:@"firstView" forKey:[NSString stringWithFormat:@"%d", i]];
}

...

-(void)buttonTappedEvent:(id)sender
{
    UIButton *tappedButton = (UIButton *)sender;

    NSLog(@"current view: %@", [viewList objectForKey:[NSString stringWithFormat:"%d", tappedButton.tag]]);
}
Zhang
  • 11,549
  • 7
  • 57
  • 87
  • "I'm aware that I can just create an array with the names of the views but I am looking for something smarter than that using the declared enum." – Segev May 26 '13 at 13:56
  • Enums are only for ordinal types apparently: http://stackoverflow.com/questions/1851567/chow-to-use-enum-for-storing-string-constants. What is your ultimate goal, what are you trying to achieve with views and enums? Dynamically load views in certain orders on the iPhone screen ? – Zhang May 26 '13 at 13:58
  • It's a bit more complex than that. Looks like an array is my only option. – Segev May 26 '13 at 14:08
0

What I normally do is declare it to dictionary for once using dispatch once

static NSDictionary* viewList = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
        viewList = [NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:1], @"firstView",[NSNumber numberWithInt:2], @"secondView",[NSNumber numberWithInt:2], @"thirdView",@"secondView",[NSNumber numberWithInt:3], @"fourthView",
nil];
    });

and find with tag like this:

-(void)function:(UIButton *)sender
{
  NSLog(@"current View: %@",[viewList objectForKey:[NSNumber numberWithInt:sender.tag]);
}
andykkt
  • 1,696
  • 16
  • 23