ios - 使用对象数据的按钮的 NSArray

标签 ios

我不确定我这样做是否正确,所以我将首先解释我正在尝试做什么。

我正在使用 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?

最佳答案

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

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

关于ios - 使用对象数据的按钮的 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17596505/

相关文章:

ios - 如何处理 Iphone 5 视网膜显示检查?

ios - 如何在 Swift 3 中使用 FacebookSDK 共享文本、链接和图像

ios - Obj-C 获取 JSON 键名

ios - 我能否在 2 月 1 日之后提交 iOS PhoneGap Build 应用程序?

ios - 错误 : git-credential-osxkeychain died of signal 11

ios - 如何执行modalSegue?

iphone - UIView 没有填满它的框架

ios - AVMutableComposition - 只播放第一首轨道(Swift)

android - Nativescript 角度项目的多选下拉或选择器节点模块

ios - 未调用 UIImagePicker 委托(delegate)