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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4476361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:20:11  来源:igfitidea点击:

Adding Items to UIToolbar Programmatically Not Working

iosios4uibarbuttonitemuitoolbar

提问by Stunner

I recently have been asking questions pertaining to UIToolbars and what not, but now I discover I need to add items to it programatically, I have seen other people's methods on how to go about doing it, but when I try doing the same thing, nothing ends up appearing. Pinpointing this problem is what I need help with. Here are my connections in IB:

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

alt text

替代文字

And here is the relevant code:

这是相关的代码:

Header file:

头文件:

#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

Implementation file:

实现文件:

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

}

Any help is greatly appreciated!

任何帮助是极大的赞赏!

EDIT:

编辑:

I removed whatever I had in the nib which would cause the toolbar to appear/be modified and I updated my code in viewDidLoad to the following:

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

And I end up getting a blank toolbar showing up. I fire up the debugger and this is what I see: enter image description hereAha! lastUpdateLabel's view's _text field is out of scope! But why? And how would I remedy this?

我最终得到了一个空白的工具栏。我启动调试器,这就是我看到的: 在此处输入图片说明啊哈!lastUpdateLabel 的视图的 _text 字段超出范围!但为什么?我该如何补救?

EDIT 2:

编辑2:

I have been able to add labels and an NSActivityIndicator with the following code:

我已经能够使用以下代码添加标签和 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];

But when I try to add a UIBarButtonItem I have no luck (doesn't show up in the toolbar):

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

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

Here is the header file:

这是头文件:

 #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

回答by Bittu

The code should work...there are couple of suggestions I could make. Instead of:

代码应该可以工作……我可以提出一些建议。代替:

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

try this:

尝试这个:

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

Also, since you are using a UINavigationController, NavControllers come with their own toolbar that you could use if you like. By default it is hidden, which you can make visible by doing this:

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

self.navigationController.toolbarHidden = NO;

and you can set its toolbar items by doing this:

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

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

Hope this tid bit helps you. good luck!

希望这小道消息能帮到你。祝你好运!

回答by Papasmurf

The buttons need to be added by the viewControllerthat is pushed by the navigationController. The navControllerholds the bar, but the items on the bar are controlled (added) by the viewControllerthat is being shown. That way, each different kind of vc has it's own bar.

这些按钮需要由viewController按下的来添加navigationController。该navController保持杆,但栏上的项目由受控(添加)viewController正被示出。这样,每种不同类型的 vc 都有自己的酒吧。

So take that barbuttonitemcode, and plunk it in the init section of the vc, and enjoy.

因此,获取该barbuttonitem代码,并将其放入 vc 的 init 部分,然后尽情享受吧。

回答by Underdog

//try this one.

//试试这个。

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

回答by Durai Amuthan.H

You can add label to tool bar directly if u have instantiated it with frame...for example

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

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

   [lbl setBackgroundColor:[UIColor clearColor]];

   [lbl setText:@"Test lbl"];

   [_toolBar addSubview:lbl];

回答by Domestic Cat

The code posted works fine, I think it has to be how the XIB is hooked up. I would re-do your connections in IB (ie break all the connections and re make them), save your XIB and try again.

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