简体   繁体   中英

create a UILabel when view appears and destroy it when view disappears

For the tutorial for my app, I am trying to create a UILabel that drifts across the displayed screen when a view appears and then is destroyed so that if the user comes back to that view during the tutorial, the UILabel will be created anew and again drift across the page. Here's a sample of one of the custom view controllers I am displaying with my UIPageViewController:

//this is just a custom UILabel with some padding
@property (nonatomic) PaddedLabel *directionsLabel;

//I have tried setting UILabel to nil or removing it from view
-(void)viewWillDisappear:(BOOL)animated
{

    NSLog(@"view is disappearing");
    //this does not remove the label
    self.directionsLabel = nil;
    //nor does this
    [self.directionsLabel removeFromSuperview];

}

- (void)viewDidAppear:(BOOL)animated
{
    [self messageTutorial];
}

- (void)messageTutorial
{

    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    CGFloat height = [[UIScreen mainScreen] bounds].size.height;
    PaddedLabel *directionsLabel = [[PaddedLabel alloc] initWithFrame:CGRectMake(_width/2, _height/2, 100, 100)];
    directionsLabel.text = @"test";

    CGRect f = directionsLabel.frame;
    f.size = size;
    directionsLabel.frame = f;
    directionsLabel.center = self.view.center;
    f = directionsLabel.frame;
    f = directionsLabel.frame;
    f.origin.y = .1*height;
    directionsLabel.frame = f;
    [self.view addSubview:directionsLabel];


   [UIView animateWithDuration:TUTORIAL_DISAPPEAR_TIME animations:^{
        directionsLabel.alpha = .5;
       CGRect f = directionsLabel.frame;
       f.origin.y = height - f.size.height*1.4;
       directionsLabel.frame = f;
       NSLog(@"animating");

    } completion:^(BOOL finished) {
        [directionsLabel removeFromSuperview];
        //this also doesn't actually remove the label

    }];


}

The problem is that if the user pages back to see this view she now sees a new label and the old one, so that if you page back and forth back and forth you end up with many many labels all saying the same thing, in different stages of progressing across the screen.

How can I remove the UILabel when the view disappears and add/create a new one when the view appears/reappears?

Thank you.

The code in your viewWillDisappear method is backwards. You need:

- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"view is disappearing");
    [self.directionsLabel removeFromSuperview];
    self.directionsLabel = nil;
}

As you had it, setting self.directionsLabel to nil before trying to remove it results in a no-op.

Also, be sure to set self.directionsLabel when you create the label.

You don't seem to be setting self.directionsLabel to anything when you create the directionsLabel inside the messageTutorial method. It is a local instance of the label there. You should set it in the method somewhere.

Afterwards, removing it from the superview in viewWillDisappear will work (tested to verify).

Instead of setting your label to nil and effectively destroying the label object (assuming automatic reference counting is on) rather use the following method to hide and show the label as and when your need it.

[self.directionsLabel setHidden:YES]; // hides it
[self.directionsLabel setHidden:NO]; // shows it

You've got the right idea setting objects you're not using to nil and removing them from the super view but it's over kill. A UILabel object uses a negligible amount of memory and you're better off creating the object once and then changing it's properties as you need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM