1

我不确定我是否以正确的方式进行此操作,所以我将首先解释我正在尝试做什么。

我正在使用来自 Annotation 对象数组的数据生成按钮。

然后我希望用户能够单击一个按钮,出现一个文本字段,并且无论他们键入什么都会覆盖相关 Annotation 对象和按钮中的文本。

所以我有注释数据生成的按钮,但这就是我卡住的地方。

//Make a button for each Annotion if x value within 0 - 1400 boundary.
    for(Annotation * ack in markerPoints)
    {
        if ([ack x] > 0 && [ack x] < 1401)
        {
            UIButton * marker = [UIButton buttonWithType:UIButtonTypeRoundedRect];

            [marker addTarget:self
                       action:@selector(markerPressed:)
             forControlEvents:UIControlEventTouchDown];
            marker.frame=CGRectMake([ack x],[ack y],100,50);
            [marker setTitle:[ack textData] forState:UIControlStateNormal ];

            [marker setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [marker setBackgroundColor:[UIColor whiteColor]];
            [self addSubview:marker ];
            [markerButtonList addObject:marker];

        }
    }

当用户按下按钮时,我如何知道它与哪个 Annotation 对象相关,以便我可以更改 Annotation textData?

4

1 回答 1

1

您可以使用数组索引值设置每个按钮的标记,并在调用方法时获取方法中的标记。例子:

//Make a button for each Annotion if x value within 0 - 1400 boundary.
NSInteger index = 0;
for(Annotation * ack in markerPoints)
{
    if ([ack x] > 0 && [ack x] < 1401)
    {
        UIButton * marker = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [marker addTarget:self
                   action:@selector(markerPressed:)
         forControlEvents:UIControlEventTouchDown];
        marker.frame=CGRectMake([ack x],[ack y],100,50);
        [marker setTitle:[ack textData] forState:UIControlStateNormal ];
        [marker setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [marker setBackgroundColor:[UIColor whiteColor]];
        [market setTag:index];
        [self addSubview:marker ];
        [markerButtonList addObject:marker];    
    }
    index++;
}

-(void)markerPressed:(UIButton*)sender{
    Annotation * ack_selected = [markerPoints objectAtIndex:sender.tag];
}
于 2013-07-11T14:51:50.623 回答