首页 文章

以编程方式创建UIButton

提问于
浏览
0

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

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

What I have tried so far: 当每个按钮被创建时,我给它一个标签和一个 Headers ,并在一个数组中记录 . 当在UIAlertView上按下'cancel'按钮时,我已经尝试检查数组中的标签,但我仍然无法实际定位UIButton .

我在想这样的事情:

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

所以,假设我有12个UIButton都称为ExhibitorBtn,但 Headers 和标签不同:

  • 对象-----名称---------- Headers --------标签

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

  • UIButton - ExhibitorBtn - 保时捷--- 2

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

Edit - added the code that creates the buttons just to clarify:

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 回答

  • 1

    为什么你不能只使用 viewWithTag:

    UIButton * button = (UIButton *)[self.scrollView viewWithTag:tag];
    
  • 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];
        }
    }
    
  • 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>;
    }
    
  • 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 {
    
      }
    
    }
    

相关问题