关于ios:以编程方式创建UIButton的目标

Targeting programmatically created UIButton

问题:如何将动态创建的uibuttons中的一个作为目标,以便更改其属性?

背景:我有一个带有uiviewconroller的故事板。当此uivc加载时,将添加一个uiscrollview,其中放置一个具有展览平面的uiimageview。每个参展商在数据库中都有一个条目,其中包含其在平面图上的位置。当UIVC加载时,所有参展商都会运行一个循环,每个UVIO都有一个UIPIN绘制。当单击一个UIB时,按钮的背景颜色被改变(以确认哪个参展商已经被选中),并且一个UIActuvVIEW显示了关于该参展商的信息。当UIAV的"取消"(OK)按钮被按下时,UIAV关闭,先前应用的背景高亮颜色应该被删除,但这里是我有问题的地方。我无法瞄准UBITCON,所以我可以改变它的背景颜色。

迄今为止我所做的尝试是:在创建每个按钮时,我给它一个标签和一个标题,并在一个数组中记录它们。当在uialertview上按下"取消"按钮时,我已经尝试检查数组中的标记,但实际上我仍然无法确定uibutton的目标。

我在想这样的事情:

1
2
// obviously not correct syntax but the kind of thing I want
[exhibitorBtn(tag) setBackgroundColor:[UIColor greenColor]];

所以,假设我有12个uibuttons,它们都称为exhiborbtn,但有不同的标题和标签:

  • 对象-----名称------标题------标记

  • uibutton—参展商btn—葛兰素胶—1

  • uibutton—参展商btn—保时捷—2

  • uibutton--exhiborbtn--rolex---3<我该如何定位该按钮的属性?

编辑-添加了创建按钮的代码,只是为了澄清:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 for (NSDictionary *dict in exhibitorStandArray) {

    NSInteger currentPosition = [exhibitorStandArray indexOfObject:dict];
    NSLog(@"Position in array = %li", (long)currentPosition);
    if (![dict[@"locCoords"] isEqual: @""]) {

        NSInteger buttonTag = [exhibitorStandArray indexOfObject:dict];
        NSString *standNo = dict[@"locStandNo"];
        NSMutableDictionary *buttonDictionary = [[NSMutableDictionary alloc] init];
        [buttonDictionary setObject:[NSNumber numberWithInteger:buttonTag] forKey:@"buttonTag"];
        [buttonDictionary setObject:standNo forKey:@"buttonStandNo"];

        [_masterButtonList addObject:buttonDictionary];

        NSString *locCoords = dict[@"locCoords"];
        NSArray *tempArray =[locCoords componentsSeparatedByString:@","];
        tlcTop = [[tempArray objectAtIndex:0] integerValue];
        tlcLeft = [[tempArray objectAtIndex:1] integerValue];
        brcTop = [[tempArray objectAtIndex:2] integerValue];
        brcRight = [[tempArray objectAtIndex:3] integerValue];
        buttonWidth = brcRight - tlcLeft;
        buttonHeight = brcTop - tlcTop;

        testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        testBtn.frame = CGRectMake(tlcTop, tlcLeft, buttonHeight, buttonWidth);
        testBtn.titleLabel.text = standNo;
        testBtn.tag = buttonTag;
        NSLog(@"UIButton Title = %@", testBtn.titleLabel.text);
        NSLog(@"UIButton Tag = %li", (long)testBtn.tag);

        testBtn.titleLabel.hidden = YES;

        [testBtn addTarget:self action:@selector(displayInfo:) forControlEvents:UIControlEventTouchUpInside];

        [_buttonArray addObject:testBtn];
        [_imageView addSubview:testBtn];
     }  
}


你为什么不能只用viewWithTag:呢?

1
    UIButton * button = (UIButton *)[self.scrollView viewWithTag:tag];


尝试以这种方式创建按钮并向其添加选择器:

1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < 5; i++)
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];

    button.tag = i;
    [button addTarget:self
               action:@selector(buttonPressedMethod:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];

    // add button to subview here
}

在方法中,做任何你想做的事情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (void) buttonPressedMethod : (id) sender {
UIButton *selectedButton = (UIButton *)sender;

  if (selectedButton.tag == 0) {

  }

  else if (selectedButton.tag == 1) {

  }


  else {

  }

}


让我们澄清一下:你把uibutton的targetaction设置为控制器的ibaction方法,并将按钮作为sender参数。现在显示uiAlertView,在它被解除后,您想向该按钮发送一些消息,对吗?

另一种方法是为uiAlertView设置delegate属性,并响应– alertView:clickedButtonAtIndex:委托方法。问题是,在那一点上,sender丢失了。您可以使用objc_setAssociatedObject将uibutton与uialertview关联,并在委托方法触发时将其检索回来:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import <objc/runtime.h>
static char myButtonKey = 0; // to use address as a key (value is irrelevant)

- (IBAction)buttonDidClick:(id)button
{
    UIAlertView *alertView = <setup alert>;
    [alertView setDelegate:self];
    objc_setAssociatedObject(alertView, &myButtonKey, button, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    <show alert>;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIButton *thatButton = objc_getAssociatedObject(alertView, &myButtonKey);
    <use thatButton>;
}


您的代码可能如下所示:

1
2
3
4
5
6
for (int i = 0; i < 5; i++)
{
    UIButton *exhibitorBtn = [[UIButton alloc] initWithFrame:etc..];
    exhibitorButton.tag = i;
    [scrollView addSubview:exhibitorBtn];
}

只需更改循环,这样每个按钮也将添加到数组中。声明NSMutableArray为属性:@property (strong, nonatomic) NSMutableArray *buttonsArray;

@synthesize并在in it方法中初始化它。buttonsArray = [[NSMutableArray alloc] init]

然后,像我上面说的那样改变循环:

1
2
3
4
5
6
7
for (int i = 0; i < 5; i++)
{
    UIButton *exhibitorBtn = [[UIButton alloc] initWithFrame:etc..];
    exhibitorButton.tag = i;
    [buttonsArray addObject:exhibitorBtn];
    [scrollView addSubview:exhibitorBtn];
}

最后,当您要访问按钮时:

1
2
3
4
5
6
7
8
for (int i = 0; i < [buttonsArray count]; i++)
{
    UIButton *button = [buttonsArray objectAtIndex:i];

    if (button.tag == 3) { // This is the button you wanted to target!
        [button setHidden:YES];
    }
}