Adding Items to UIToolbar Programmatically Not Working
Asked Answered
P

5

5

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
Parrakeet answered 18/12, 2010 at 2:58 Comment(0)
T
12

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!

Tatiania answered 10/1, 2011 at 18:3 Comment(2)
Thanks, I found the self.navigationController.toolbarHidden = NO; to be of use/work for me. However I am still experiencing a blank toolbar. Please see the edits I made to my question, above.Parrakeet
it seems you are using two different toolbars. if you want to use the navigation toolbar, you have to mention it using "self.navigationController.toolbar". If you are using your own toolbar then you can mention that using "self.toolbar", if its been setup as a property. your subviews seems to be added to your own toolbar but the items are being set for the navigation toolbar. thats where I would start looking for the problem.Tatiania
H
2

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.

Hendon answered 15/6, 2011 at 0:4 Comment(0)
A
1

//try this one.

- (void)viewDidAppear:(BOOL)animated
{
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
}
Autoerotism answered 7/7, 2011 at 6:40 Comment(0)
W
0

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.

Waugh answered 18/12, 2010 at 3:31 Comment(1)
Did what you suggested, no luck. I just see a plain grey toolbar. Throwing in this line of code: toolbar.barStyle = UIBarStyleBlackOpaque; actually gets the toolbar to turn black, so the connections aren't messed up or anything... Thanks for the response though.Parrakeet
U
0

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];
Ulcer answered 16/7, 2013 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.