0

我有动态 UIButtons 子视图是 UIlabel。我的要求是第一次在一个按钮中显示一个标签文本,之后根据用户选择(长按按钮)需要更新特定按钮子视图标签文本并删除其余按钮子视图标签文本。

我试过这样

for (int i=0; i < [arr count]; i++) 
{
          UILabel *myLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 60, 21)];
        if (myissue.tag ==1) {
            myLbl.text = @"Default";
        }else {
            myLbl.text = @"";
        }
        myLbl.backgroundColor = [UIColor clearColor];
        myLbl.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
        [myLbl setFont:[UIFont fontWithName:@"Helvetica" size:12]];
        myLbl.textAlignment = NSTextAlignmentCenter;
        myLbl.tag = i+1;
        [myButton addSubview:defaultLbl];
        [myLbl release];
}

And for retrieving the UILabel text 
- (void)longPressTap:(UILongPressGestureRecognizer *)sender
{
if ([recognizer.view tag]) {
   for (UIButton *btn in scrollView.subviews) {
                UIButton *btnTag = (UIButton *)btn;
                 NSLog(@"--sv:%@", btn.subviews);
                 if (recognizer.view.tag == btnTag.tag){
                      [[btn.subviews objectAtIndex:3] text] ;
                    }else {

                        [[btn.subviews objectAtIndex:3] textAttributesForNil] ;
                }
            }
        }
    }

}

我的问题是我无法选择按钮子视图标签文本并删除其余按钮标签文本。请帮我。

4

1 回答 1

1

您不需要添加标签作为按钮的子视图,按钮已经有自己的文本标签,可以使用[button setTitle:(NSString *) forState:(UIControlState)]

并删除刚刚设置的文本seTitle:@""

于 2013-03-06T07:24:57.023 回答