0

我已经像这样声明了一个枚举:

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

我的目标是 aUIButton将触发另一个函数,uiButton sender.tag并且该函数将知道将整数转换为正确的视图。我知道我可以使用视图名称创建一个数组,但我正在寻找比使用声明的枚举更智能的东西。

例子:

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

谢谢

4

3 回答 3

3

好吧,最好的解决方案是实际存储视图。您还可以使用 aIBOutletCollection创建数组。声明 anenum只是另一种存储名称的方式。

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

...

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

...

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

PS:转换tagenum是微不足道的,它只是 myViews viewName = sender.tag,可能带有演员表myViews viewName = (myViews) sender.tag

于 2013-05-26T15:31:45.960 回答
0

将它存储在 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]]);
}
于 2013-05-26T13:55:10.230 回答
0

我通常做的是使用 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];
    });

并找到这样的标签:

-(void)function:(UIButton *)sender
{
  NSLog(@"current View: %@",[viewList objectForKey:[NSNumber numberWithInt:sender.tag]);
}
于 2013-05-26T14:08:10.647 回答