开发者

Local Variable addSubView

example if there is a method addLabel:

- (void)addLabel {
   for (NSInteger i = 0; i < 5; i ++) {
       UILabel *label = [[UILabel alloc] init];
       [label setText:@"label"];
       [[self 开发者_StackOverflow中文版view] addSubView:label];
       [label release];
   }
}

and the method is called whenever a button is fired. Does it need to remove all the label from the subviews first (removeFromSuperView:) before addSubview again?


First, you have to give some coordinate to UILabel. So that, it can display at proper place.

You can use following line for that:

UILabel *lblTaskTitle = [[UILabel alloc] initWithFrame:CGRectMake(45.0, 5, 200.0, 35.0)];

Another thing is that, it will be better if you remove other label. (It's not necessary, but it's good practice).

You can do it in following way:

    UILabel *lbl = nil;

    NSArray *Arraylbl = [self.view subviews];
    for (lbl in Arraylbl){
        if ([lbl isKindOfClass:[UILabel class]]){
            [lbl removeFromSuperview];
        }
    }

Hope it will be fine for you.

Let me know in case of any difficulty.


Yes you have to remove all the previous labels from super view otherwise they all will be added above the previous existing labels, so the new labels would not be understandable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜