如何删除以编程方式创建的标签?

时间:2012-11-28 05:45:41

标签: iphone objective-c ios

我正在使用此代码以编程方式创建两个标签..

-(void)addLabel:(id)sender
{
    ExampleAppDataObject* theDataObject = [self theAppDataObject]; 
    theDataObject.count = theDataObject.count+1;
    NSLog(@"count is :%i",theDataObject.count);

    if (theDataObject.count == 2) {
        addLabel.enabled = NO;
    }
    if (theDataObject.count == 1) {
        CGRect imageFrame = CGRectMake(10, 10, 150, 80);
        labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
        blabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 100, 100)];

        blabel.text = @"Write here";
        //alabel.text = self.newsAsset.title;
        blabel.adjustsFontSizeToFitWidth = NO;
        blabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        blabel.font = [UIFont boldSystemFontOfSize:18.0];
        blabel.textColor = [UIColor blueColor];    
        // alabel.shadowColor = [UIColor whiteColor];
        // alabel.shadowOffset = CGSizeMake(0, 1);
        blabel.backgroundColor = [UIColor clearColor];
        blabel.lineBreakMode = UILineBreakModeWordWrap;
        blabel.numberOfLines = 10;
        blabel.minimumFontSize = 8.;
        blabel.adjustsFontSizeToFitWidth = YES;
        [blabel sizeToFit];
        labelResizableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;



        // enable touch delivery
        blabel.userInteractionEnabled = YES;

        //tao gasture recognizer for label
        UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blabelTap:)]; 
        doubleTap.numberOfTapsRequired = 2; 
        [blabel addGestureRecognizer:doubleTap];

        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
                                                          initWithTarget:self
                                                          action:@selector(longPress:)];
        [longPressGesture setMinimumPressDuration:1];
        [blabel addGestureRecognizer:longPressGesture];

        //Calculate the expected size based on the font and linebreak mode of your label
        CGSize maximumLabelSize = CGSizeMake(296,9999);

        CGSize expectedLabelSize = [greetString sizeWithFont:blabel.font 
                                           constrainedToSize:maximumLabelSize 
                                               lineBreakMode:blabel.lineBreakMode]; 


        //adjust the label the the new height.
        CGRect newFrame = blabel.frame;
        newFrame.size.height = expectedLabelSize.height+40;
        newFrame.size.width = expectedLabelSize.width+40;

        blabel.frame = newFrame;
        labelResizableView.frame = newFrame;

        labelResizableView.contentView = blabel;
        labelResizableView.delegate = self;
        labelResizableView.tag =2;
        [self.view addSubview:labelResizableView];
    }else if (theDataObject.count == 2) {
        CGRect imageFrame = CGRectMake(10, 10, 150, 80);
        labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
        clabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 100, 100)];

        clabel.text = @"Write here";
        //alabel.text = self.newsAsset.title;
        clabel.adjustsFontSizeToFitWidth = NO;
        clabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        clabel.font = [UIFont boldSystemFontOfSize:18.0];
        clabel.textColor = [UIColor blueColor];    
        // alabel.shadowColor = [UIColor whiteColor];
        // alabel.shadowOffset = CGSizeMake(0, 1);
        clabel.backgroundColor = [UIColor clearColor];
        clabel.lineBreakMode = UILineBreakModeWordWrap;
        clabel.numberOfLines = 10;
        clabel.minimumFontSize = 8.;
        clabel.adjustsFontSizeToFitWidth = YES;
        [clabel sizeToFit];
        labelResizableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;



        // enable touch delivery
        clabel.userInteractionEnabled = YES;

        //tao gasture recognizer for label
        UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clabelTap:)]; 
        doubleTap.numberOfTapsRequired = 2; 
        [clabel addGestureRecognizer:doubleTap];

        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
                                                          initWithTarget:self
                                                          action:@selector(longPress:)];
        [longPressGesture setMinimumPressDuration:1];
        [clabel addGestureRecognizer:longPressGesture];

        //Calculate the expected size based on the font and linebreak mode of your label
        CGSize maximumLabelSize = CGSizeMake(296,9999);

        CGSize expectedLabelSize = [greetString sizeWithFont:clabel.font 
                                           constrainedToSize:maximumLabelSize 
                                               lineBreakMode:clabel.lineBreakMode]; 


        //adjust the label the the new height.
        CGRect newFrame = blabel.frame;
        newFrame.size.height = expectedLabelSize.height+40;
        newFrame.size.width = expectedLabelSize.width+40;

        clabel.frame = newFrame;
        labelResizableView.frame = newFrame;

        labelResizableView.contentView = clabel;
        labelResizableView.delegate = self;

        labelResizableView.tag=3;
        [self.view addSubview:labelResizableView];
    }


}

当用户长按按钮时,它将被删除...

- (void)longPress:(UILongPressGestureRecognizer *)longPressGesture {

    if (longPressGesture.state == UIGestureRecognizerStateEnded) {
        //NSLog(@"Long press Ended");

       // NSLog(@"blabel long pressed");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete Label" message:@"Want delete label" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
        [alert show];

    }
    else {
        //NSLog(@"Long press detected.");
    }


}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        ExampleAppDataObject* theDataObject = [self theAppDataObject];
        if (theDataObject.count!=0) {
            theDataObject.count = theDataObject.count-1;
        }
        addLabel.enabled = YES;
        [labelResizableView removeFromSuperview];
       // NSLog(@"yes btn tapped");
    }
}

但是现在当我使用longbelree blabel而不是clabel被删除时它将永远不会删除blabel.thanx。

5 个答案:

答案 0 :(得分:4)

使用 Tag 属性删除 labelResizableView

-(void)addLabel:(id)sender
{
  labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
  labelResizableView.tag = 100;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        ExampleAppDataObject* theDataObject = [self theAppDataObject];
        if (theDataObject.count!=0) {
            theDataObject.count = theDataObject.count-1;
        }
        addLabel.enabled = YES;
        UILabel *tempLabel = (UILabel *)[self.view viewWithTag:100];
        if(tempLabel)
            [tempLabel removeFromSuperview];

    }
}

答案 1 :(得分:1)

希望此代码可以帮助您:)

NSArray *subArray =  [self.view subviews];
    if([subArray count] != 0) {
        for(int i = 0 ; i < [subArray count] ; i++) {           
    [[subArray objectAtIndex:i] removeFromSuperview];        
 }
    }

答案 2 :(得分:1)

在视图中添加控件:

[self.view addsubview:yourcontrolid];

例如:

[self.view addsubview:labelid];

从视图中添加控件:

[controlid removefromsuperview];

[labelid removefromsuperview];

答案 3 :(得分:0)

您正在添加:

    [self.view addSubview:labelResizableView];

将其删除labelResizableView,并释放clabel或blabel,无论你的情况如何。

也许this举了一个例子

答案 4 :(得分:0)

这是因为你的代码

else if (theDataObject.count == 2) {

正在调用,在此代码中您正在添加

 labelResizableView.contentView = clabel;  

然后你将它添加到你的视图

[self.view addSubview:labelResizableView];  

因此,当您删除labelResizableView

[labelResizableView removeFromSuperview];

因此,结果是您要添加labelResizableView 2次并删除具有clabel的labelResizableView

相关问题