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

Stu*_*ner 5 uitoolbar uibarbuttonitem ios4 ios

我最近一直在询问有关UIToolbars的问题,但是现在我发现我需要以编程方式添加项目,我已经看到了其他人如何去做的方法,但是当我尝试做同样的事情时,没有什么最终出现.找出这个问题是我需要帮助的.以下是我在IB的关系:

替代文字

以下是相关代码:

头文件:

#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
Run Code Online (Sandbox Code Playgroud)

实施文件:

- (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];

}
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏!

编辑:

我删除了笔尖中的任何内容,这会导致工具栏出现/被修改,我将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]];
Run Code Online (Sandbox Code Playgroud)

我最终得到一个空白的工具栏显示.我启动了调试器,这就是我所看到的: 在此输入图像描述 啊哈!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];
Run Code Online (Sandbox Code Playgroud)

但是当我尝试添加UIBarButtonItem时,我没有运气(没有显示在工具栏中):

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

这是头文件:

 #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
Run Code Online (Sandbox Code Playgroud)

Bit*_*ttu 12

代码应该工作......我可以提出几点建议.代替:

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
Run Code Online (Sandbox Code Playgroud)

试试这个:

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel] animated:YES];
Run Code Online (Sandbox Code Playgroud)

此外,由于您使用的是UINavigationController,因此NavControllers附带了您自己的工具栏,如果您愿意,可以使用它们.默认情况下,它是隐藏的,您可以通过执行以下操作使其可见:

self.navigationController.toolbarHidden = NO;
Run Code Online (Sandbox Code Playgroud)

并且您可以通过执行以下操作来设置其工具栏项:

[self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]];
Run Code Online (Sandbox Code Playgroud)

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

  • 看来你正在使用两个不同的工具栏.如果要使用导航工具栏,则必须使用"self.navigationController.toolbar"来提及它.如果您使用自己的工具栏,那么您可以使用"self.toolbar",如果它已设置为属性.您的子视图似乎已添加到您自己的工具栏中,但正在为导航工具栏设置项目.那就是我开始寻找问题的地方. (2认同)

Dom*_*Cat 0

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