简体   繁体   English

UILabel在UIViewControllers的底部栏中显示和消失

[英]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: 我想将UILabel添加到导航控制器推送和弹出的所有UIViewControllers的底部工具栏中:

- (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. 另一个按钮(sortButton)仍然可见。

What should I do to keep the label visible ? 如何使标签可见?

thanks 谢谢

Your problem is this: self.titleLabel. 您的问题是: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. 多个labelButton实例都引用该视图,并且当您设置工具栏项目时(我假设动画== YES),titleLabel将从您不希望看到的其超级视图中删除。

Create a new titleLabel for each usage and your problem will go away. 为每种用法创建一个新的titleLabel,您的问题将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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