以编程方式将项添加到UIToolbar不起作用

时间:2010-12-18 02:58:41

标签: ios ios4 uibarbuttonitem uitoolbar

我最近一直在询问有关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];

}

非常感谢任何帮助!

编辑:

我删除了笔尖中的任何内容,这会导致工具栏显示/被修改,我将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:

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

@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 个答案:

答案 0 :(得分:12)

代码应该有效......我可以提出几点建议。而不是:

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

试试这个:

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

此外,由于您使用的是UINavigationController,因此NavControllers自带工具栏,您可以根据需要使用它们。默认情况下,它是隐藏的,您可以通过执行此操作使其可见:

self.navigationController.toolbarHidden = NO;

您可以通过以下方式设置其工具栏项:

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

希望这个有点帮助你。祝你好运!

答案 1 :(得分:1)

按钮需要由viewController推送的navigationController添加。 navController保留栏,但栏上的项目由正在显示的viewController控制(添加)。这样,每种不同类型的vc都有它自己的栏。

所以请使用barbuttonitem代码,并将其插入vc的init部分,然后享受。

答案 2 :(得分:1)

//尝试这个。

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

答案 3 :(得分:0)

发布的代码工作正常,我认为它必须是XIB如何连接起来的。我会在IB中重新建立连接(即断开所有连接并重新建立它们),保存您的XIB并重试。

答案 4 :(得分:0)

如果你已经用框架实例化它,你可以直接向工具栏添加标签......例如

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

   [lbl setBackgroundColor:[UIColor clearColor]];

   [lbl setText:@"Test lbl"];

   [_toolBar addSubview:lbl];