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

发布于 2024-10-08 19:52:33 字数 5062 浏览 1 评论 0原文

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

最终出现一个空白工具栏。我启动调试器,这就是我所看到的: 在此处输入图像描述 啊哈! 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

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:

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:

    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 here
Aha! lastUpdateLabel's view's _text field is out of scope! But why? And how would I remedy this?

EDIT 2:

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

@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):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小伙你站住 2024-10-15 19:52:33

该代码应该可以工作...我可以提出一些建议。而不是:

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

尝试这个:

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

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

self.navigationController.toolbarHidden = NO;

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

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

希望这一点对您有所帮助。祝你好运!

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:

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!

弃爱 2024-10-15 19:52:33

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

因此,将 barbuttonitem 代码放入 vc 的 init 部分,然后享受吧。

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

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

┼── 2024-10-15 19:52:33

//试试这个。

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

//try this one.

- (void)viewDidAppear:(BOOL)animated
{
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
}
怪我鬧 2024-10-15 19:52:33

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

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.

︶葆Ⅱㄣ 2024-10-15 19:52:33

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

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

   [lbl setBackgroundColor:[UIColor clearColor]];

   [lbl setText:@"Test lbl"];

   [_toolBar addSubview:lbl];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文