2

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

-(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");
    }
}

但是现在当我 longpree blabel 时仍然删除 clabel 并且它永远不会提前删除 blabel.thanx。

4

5 回答 5

5

使用该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];

    }
}
于 2012-11-28T05:52:30.127 回答
1

在视图中添加控件:

[self.view addsubview:yourcontrolid];

前任:

[self.view addsubview:labelid];

要从您的视图中添加控制:

[controlid removefromsuperview];

前任

[labelid removefromsuperview];
于 2012-11-28T06:24:06.460 回答
1

希望这段代码对你有帮助:)

NSArray *subArray =  [self.view subviews];
    if([subArray count] != 0) {
        for(int i = 0 ; i < [subArray count] ; i++) {           
    [[subArray objectAtIndex:i] removeFromSuperview];        
 }
    }
于 2012-11-28T06:05:29.867 回答
0

您正在添加:

    [self.view addSubview:labelResizableView];

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

也许给出了一个例子

于 2012-11-28T05:52:45.927 回答
0

这是因为你的代码

else if (theDataObject.count == 2) {

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

 labelResizableView.contentView = clabel;  

然后您将其添加到您的视图中

[self.view addSubview:labelResizableView];  

所以当你删除labelResizableView

[labelResizableView removeFromSuperview];

所以结果是你添加了labelResizableView2 次并删除了labelResizableView有 clabel 的。

于 2012-11-28T06:05:58.880 回答