-1

ボタンをforループに手動で追加しました。その後、ボタンを非表示または変更してラベルを非表示にする方法は?

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[button setTitle:@"My Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

UILabel *lblFileName = [[UILabel alloc] init];
lblFileName.text = [[objectArray objectAtIndex:i] valueForKey:@"fileName"];
lblFileName.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblFileName];

-(IBAction)myAction:(id)sender
{
    // hide the button or change title button and hide label
}
4

6 に答える 6

2

を追加buttons & labelsしている場合はfor loop、次の解決策を試してください。

button.tag = i + 1;//0 is default tag for all views.
lblFileName.tag = i + 1;

-(IBAction)myAction:(id)sender
{
    UIButton *btn= (UIButton *)sender;
    [btn setHidden:YES];// hide the button
    btn.titleLabel.text = [[objectArray objectAtIndex:[sender tag]] valueForKey:@"fileName"];

     //Get the label of selected button using button tag.
    [[self.view viewWithTag:[sender tag]] setHidden:YES];

}
于 2013-05-22T05:50:44.303 に答える
0

Girish が言ったように、各ボタンとラベルに一意のタグを付けます。2 つのタグが同じであってはなりません。

10 個のボタンと 10 個のラベルがあるとします。次に、ボタン 1 ~ 10 とラベル 11 ~ 20 にタグを付けます。

次に、メソッドで、対応するタグを使用して任意のラベルまたはボタンにアクセスできます。

UIButton *button=(UIButton *)[self.view viewWithTag:1];
UILabel *label=(UILabel *)[self.view viewWithTag:11];
于 2013-05-22T07:14:06.710 に答える
0
-(IBAction)myAction:(id)sender
{
    // hide the button or change title button
[button setHidden:YES];
}
于 2013-05-22T05:51:50.097 に答える