簡體   English   中英

定位以編程方式創建的UIButton

[英]Targeting programmatically created UIButton

問題:如何定位許多動態創建的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-參展商Btn-葛蘭素------ 1

  • UIButton-參展商Btn-保時捷--- 2

  • UIButton-ExhibitionorBtn-Rolex ------- 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];
     }   
}

為什么不能只使用viewWithTag:

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

您的代碼可能看起來像這樣:

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

讓我們說清楚:您確實將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>;
}

嘗試以這種方式創建按鈕並向其添加選擇器:

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 {

  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM