以编程方式创建UIButton

时间:2014-02-13 09:56:01

标签: ios objective-c cocoa-touch uibutton programmatically-created

问题:如何定位许多动态创建的UIButton中的一个,以便我可以更改其属性?

后台:我有一个带有UIViewConroller的故事板。当这个UIVC加载时,添加了一个UIScrollView,其中放置了一个具有展览平面图的UIImageView。每个参展商都有一个数据库中的条目,其中包含其在平面图上的位置。当加载UIVC时,为所有参展商运行循环,并且每个参展商都在UIIV上绘制UIButton。单击UIB时,按钮背景颜色会发生变化(以确认选择了哪个参展商),并显示UIAlertView,其中包含有关该参展商的信息。当按下UIAV的“取消”(确定)按钮时,UIAV关闭,之前应用的背景突出显示颜色应该被删除,但这里是我遇到问题的地方。我无法定位UIButton,因此我可以更改其背景颜色。

到目前为止我尝试了什么:在创建每个按钮时,我会给它一个标签和一个标题并以数组形式记录。当在UIAlertView上按下“取消”按钮时,我已经尝试检查数组中的标签,但我仍然无法实际定位UIButton。

我在想这样的事情:

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

所以,假设我有12个UIButtons都叫ExhibitorBtn,但标题和标签不同:

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

  • UIButton - ExhibitorBtn - Glaxo ------ 1

  • UIButton - ExhibitorBtn - Porsche --- 2

  • UIButton - ExhibitorBtn - 劳力士------- 3<我该如何定位该按钮的属性?

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

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

4 个答案:

答案 0 :(得分:1)

为什么不能只使用viewWithTag:

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

答案 1 :(得分:0)

您的代码可能如下所示:

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并在init方法中初始化它。 buttonsArray = [[NSMutableArray alloc] init]

然后,改变循环,如上所述:

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

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

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

答案 2 :(得分:0)

让我们说清楚:你确实将UIButton的targetaction设置为控制器的IBAction方法,并将按钮作为sender参数。现在你显示UIAlertView和之后被解雇,你想要发送一些消息到那个按钮,对吗?

另一种方法是为UIAlertView设置delegate属性并响应– alertView:clickedButtonAtIndex:委托方法。麻烦的是sender在那时失去了。您可以使用objc_setAssociatedObject将UIButton与UIAlertView关联,并在委托方法触发时将其检索回来:

#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>;
}

答案 3 :(得分:0)

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

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
}

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

- (void) buttonPressedMethod : (id) sender {
UIButton *selectedButton = (UIButton *)sender;

  if (selectedButton.tag == 0) {

  }

  else if (selectedButton.tag == 1) {

  }


  else {

  }

}