以编程方式向UIToolbar添加项目不起作用

5

我最近一直在问有关UIToolbars的问题,但现在我发现我需要在程序中添加项目,我看到其他人处理此事的方法,但当我尝试做同样的事情时,什么也没有出现。我需要帮助解决这个问题。以下是我在IB中的连接:

alt text

以下是相关代码:

头文件:

#import <UIKit/UIKit.h>

@interface ParkingRootViewController : UIViewController {
    UINavigationController *navigationController;
    UIToolbar *toolbar;
    UIBarButtonItem *lastUpdateLabel;
}

@property(nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel;

- (IBAction)selectHome:(id)sender;

@end

实现文件:

- (void)viewDidLoad {
        [super viewDidLoad];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
    label.text = @"last updated...";
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:13.0];
    label.userInteractionEnabled = NO;

    lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
    [label release];
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];


    [self.view addSubview:self.navigationController.view];
    //[self.view addSubview:toolbar];
    //[self.navigationController.view addSubview:toolbar];

    [self.navigationController.view setFrame:self.view.frame];

}

非常感谢您的帮助!

编辑:

我已经删除了nib文件中可能导致工具栏出现/被修改的内容,并且我在viewDidLoad方法中更新了我的代码,如下所示:

    self.navigationController.toolbarHidden = NO;

    //creating label in tool bar 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
    label.text = @"last updated...";
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    //label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0];
    //label.highlighted = YES;
    label.font = [UIFont systemFontOfSize:13.0];
    label.userInteractionEnabled = NO;

    UIBarButtonItem *lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
    //[lastUpdateLabel initWithCustomView:label];
    //[label release];
    //[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
    [self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]];

我最终得到了一个空白的工具栏。我启动调试器,看到以下内容:

enter image description here

啊哈!lastUpdateLabel的视图的_text字段超出了范围!但是为什么?我该如何解决?

编辑2:

我已经能够使用以下代码添加标签和NSActivityIndicator:

@synthesize refreshDataButton;
//...
self.navigationController.toolbarHidden = NO;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 80.0f, 40.0f)];
    label.text = @"last updated...";
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13.0];
    label.userInteractionEnabled = NO;
    [self.toolbar addSubview:label];

// create activity indicator
    //                        dist frm lft, dist frm top
    CGRect frame = CGRectMake(   90.0,         11.0,      25.0, 25.0);      
    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];   
    loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; 
    [loading sizeToFit];    
    loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
                                UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | 
                                UIViewAutoresizingFlexibleBottomMargin);    
    [loading startAnimating];

    [self.toolbar addSubview:loading];

但是当我尝试添加一个UIBarButtonItem时,我没有成功(在工具栏中没有显示):

self.refreshDataButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(refreshDataButtonTapped)];
[self setToolbarItems:[NSArray arrayWithObject:refreshDataButton]];

这是头文件:
 #import <UIKit/UIKit.h>
//#import <CoreData/CoreData.h>

@interface ParkingRootViewController : UIViewController {
    UINavigationController *navigationController;
    UIToolbar *toolbar;
    UIBarButtonItem *refreshDataButton;
    //UIActivityIndicatorView *loading;
}

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) UIBarButtonItem *refreshDataButton;
//@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loading;


@property (nonatomic, readonly) NSString *applicationDocumentsDirectory;

-(IBAction)selectHome:(id)sender;
-(void)testCoreData;
-(void)refreshDataButtonTapped;

@end
5个回答

12

这段代码应该可以运行...我有几个建议。 相比于:

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];

试试这个:

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel] animated:YES];

另外,由于您使用的是UINavigationController,因此NavControllers带有自己的工具栏,如果需要的话,您可以使用它。默认情况下,它是隐藏的,通过执行以下操作,您可以将其显示出来:

self.navigationController.toolbarHidden = NO;

你可以通过以下方式设置工具栏项目:

[self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]];

希望这个小技巧能够帮助你。祝你好运!


谢谢,我发现self.navigationController.toolbarHidden = NO;对我有用。但是我仍然遇到空白工具栏的问题。请参见我在上面提问中所做的编辑。 - Stunner
2
看起来你正在使用两个不同的工具栏。如果你想使用导航工具栏,你必须使用"self.navigationController.toolbar"进行说明。如果你正在使用自己的工具栏,那么你可以使用"self.toolbar"进行说明,如果它已经被设置为属性。你的子视图似乎被添加到了你自己的工具栏中,但是项目却被设置为导航工具栏。这就是我开始寻找问题的地方。 - Bittu

2
按钮需要由被navigationController推送的viewController添加。 navController持有该栏,但栏上的项目由正在显示的viewController控制(添加)。这样,每种不同类型的vc都有自己的栏。
因此,将barbuttonitem代码放入vc的init部分,并享受它。

1

//试试这个。

- (void)viewDidAppear:(BOOL)animated
{
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
}

0

如果您已经使用框架实例化了工具栏,那么可以直接向其添加标签...例如:

   UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(7,7,200,30)];

   [lbl setBackgroundColor:[UIColor clearColor]];

   [lbl setText:@"Test lbl"];

   [_toolBar addSubview:lbl];

0

发布的代码运行良好,我认为问题可能出在XIB的连接上。建议您在IB中重新连接(即断开所有连接并重新建立它们),保存XIB文件后再试一次。


按照您的建议尝试了,但没有成功。我只看到一个普通的灰色工具栏。加入这行代码:toolbar.barStyle = UIBarStyleBlackOpaque; 实际上让工具栏变成了黑色,所以连接并没有出现问题...还是感谢您的回复。 - Stunner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接