简体   繁体   中英

UILabel appears and disappears from the bottom bar of UIViewControllers

I want to add a UILabel to the bottom toolbar of all UIViewControllers pushed and popped by the navigation controller:

- (void)init
{        
    //Bottom toolbar label
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, 320, 21.0f)];
    [self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    [self.titleLabel setText:@"Selected Comics: 0"];
    [self.titleLabel setTextAlignment:UITextAlignmentLeft];

}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{

    UIBarButtonItem *labelButton = [[UIBarButtonItem alloc] initWithCustomView:self.titleLabel];

    UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];

    [viewController setToolbarItems:[NSArray arrayWithObjects:labelButton, flex, sortButton, nil] animated:animated];

    [labelButton release];
}

However, after I've pushed and popped a view controller, the label appears and immediately disappears. The other button (sortButton) remains visible instead.

What should I do to keep the label visible ?

thanks

Your problem is this: self.titleLabel.

Multiple labelButton instances are all referencing that view, and when you set the toolbar itmes (animated == YES I assume), titleLabel is getting removed from its superview where you don't expect it.

Create a new titleLabel for each usage and your problem will go away.

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