2

このコードを使用して、プログラムで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");
    }
}

しかし、今でもblabelをlongpreeすると、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

このコードがお役に立てば幸いです:)

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 に答える
1

ビューにコントロールを追加するには:

[self.view addsubview:yourcontrolid];

元:

[self.view addsubview:labelid];

ビューからコントロールを追加するには:

[controlid removefromsuperview];

[labelid removefromsuperview];
于 2012-11-28T06:24:06.460 に答える
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回追加し、 labelResizableViewclabelを持つものを削除することになります。

于 2012-11-28T06:05:58.880 に答える