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

最善の解決策は、実際にビューを保存することです。IBOutletCollectionaを使用して配列を作成することもできます。an の宣言は、enum名前を格納するもう 1 つの方法です。

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.tagmyViews 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

私が通常行うことは、一度ディスパッチを使用して一度辞書に宣言することです

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 に答える