Ios 使用对象数据的按钮数组

Ios 使用对象数据的按钮数组,ios,Ios,我不确定我的方法是否正确,所以我先解释一下我想做什么 我使用注释对象数组中的数据生成按钮 然后,我希望用户能够单击一个按钮,显示一个文本字段,并且无论他们键入什么,都将覆盖相关注释对象中的文本以及按钮 所以我有由注释数据生成的按钮,但这就是我被卡住的地方 //Make a button for each Annotion if x value within 0 - 1400 boundary. for(Annotation * ack in markerPoints) {

我不确定我的方法是否正确,所以我先解释一下我想做什么

我使用注释对象数组中的数据生成按钮

然后,我希望用户能够单击一个按钮,显示一个文本字段,并且无论他们键入什么,都将覆盖相关注释对象中的文本以及按钮

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

//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];

        }
    }
//如果x值在0-1400范围内,则为每个注释制作一个按钮。
用于(注释*标记点中的确认)
{
如果([ack x]>0&[ack x]<1401)
{
UIButton*标记=[UIButton Button,类型:UIButtonyPeroundRect];
[目标:自我
操作:@选择器(标记压缩:)
forControlEvents:UIControlEventTouchDown];
marker.frame=CGRectMake([ack x],[ack y],100,50);
[marker setTitle:[ack textData]用于状态:UIControlStateNormal];
[marker setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[marker setBackgroundColor:[UIColor whiteColor]];
[自添加子视图:标记];
[markerButtonList addObject:marker];
}
}

当用户按下按钮时,我如何判断它与哪个注释对象相关,以便我可以更改注释文本数据?

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

//如果x值在0-1400范围内,则为每个注释制作一个按钮。
NSInteger指数=0;
用于(注释*标记点中的确认)
{
如果([ack x]>0&[ack x]<1401)
{
UIButton*标记=[UIButton Button,类型:UIButtonyPeroundRect];
[目标:自我
操作:@选择器(标记压缩:)
forControlEvents:UIControlEventTouchDown];
marker.frame=CGRectMake([ack x],[ack y],100,50);
[marker setTitle:[ack textData]用于状态:UIControlStateNormal];
[marker setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[marker setBackgroundColor:[UIColor whiteColor]];
[市场集合标签:指数];
[自添加子视图:标记];
[markerButtonList addObject:marker];
}
索引++;
}
-(无效)标记按:(UIButton*)发送者{
注释*ack_selected=[markerPoints objectAtIndex:sender.tag];
}

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

//如果x值在0-1400范围内,则为每个注释制作一个按钮。
NSInteger指数=0;
用于(注释*标记点中的确认)
{
如果([ack x]>0&[ack x]<1401)
{
UIButton*标记=[UIButton Button,类型:UIButtonyPeroundRect];
[目标:自我
操作:@选择器(标记压缩:)
forControlEvents:UIControlEventTouchDown];
marker.frame=CGRectMake([ack x],[ack y],100,50);
[marker setTitle:[ack textData]用于状态:UIControlStateNormal];
[marker setTitleColor:[UIColor blackColor]用于状态:UIControlStateNormal];
[marker setBackgroundColor:[UIColor whiteColor]];
[市场集合标签:指数];
[自添加子视图:标记];
[markerButtonList addObject:marker];
}
索引++;
}
-(无效)标记按:(UIButton*)发送者{
注释*ack_selected=[markerPoints objectAtIndex:sender.tag];
}

但是它是一个NSMutableArray,它还有索引值吗?我还需要能够删除按钮。并确保标记值为!=0,因此您不会出现意外行为。+1。答案相同,但更新速度比我快一点;)@另一个新手肯定是一个
NSMutableArray
对象有一个索引,因为它仍然是一个数组,所以是一个排序列表,只是能够动态变化;)您考虑的是
NSSet
,一个未排序的列表;)但是它是一个NSMutableArray,它还有索引值吗?我还需要能够删除按钮。并确保标记值为!=0,因此您不会出现意外行为。+1。答案相同,但更新速度比我快一点;)@另一个新手肯定是一个
NSMutableArray
对象有一个索引,因为它仍然是一个数组,所以是一个排序列表,只是能够动态变化;)您考虑的是
NSSet
,一个未排序的列表;)
//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];
}