tags:

views:

364

answers:

2

I have an UITableViewController which I would like to add UIToolbar to with one button. In the

- (void)viewDidLoad;

method of UITableViewController I have:

- (void)viewDidLoad {
[super viewDidLoad];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                             target:self
                                                                             action:@selector(pressButton1:)];

self.navigationItem.title = @"Some title";
self.navigationItem.leftBarButtonItem = button;
 }

Unfortunately I don't see the toolbar when I run my app. Any hints? Should I do something more?

+1  A: 

Your method requires an autorelease:

- (void)viewDidLoad {
[super viewDidLoad];

UIBarButtonItem *button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pressButton1:)] autorelease];

self.navigationItem.title = @"Some title";
self.navigationItem.leftBarButtonItem = button;
 }

There's nothing wrong with your code per se. Your question states you want to add an UIToolBar to your view? Really? Or do you just want to add a button to the NavigationItem for UITableView?

Jordan
Thanks Jordan!In the beginning I wanted to have UIToolbar in my view togeether with UITable View, but later on I figured out that I could leverage the NavigationItem from UITTableViewController and I came out with the code above. If there is nothing wrong, then any ideas why I don't see the toolbar?
Jakub
Do you check your IB Connections? What does show? Maybe you can post some code?
Jordan
+2  A: 

The navigationItem property of a view controller is useless if that controller is not displayed inside a UINavigationController.
If your view controller is inside a navigation controller I don't know what the problem is.
Otherwise you can use an UINavigationItem but you need to create a UINavigationBar yourself. Either in the Interface Builder (add a UINavigationBar and add a UINavigationItem, then connect the UINavigationItem to a property outlet declared your view controller (you don't need to connect the Bar).
Or in your code :

UIBarButtonItem *item = [[UIBarButtonItem alloc] 
                             initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                             target:self action:@selector(pressButton1:)];

UINavigationItem* navItem = [[UINavigationItem alloc] init];
navItem.rightBarButtonItem = item;
navItem.title = @"Your title";

naviBar = [[UINavigationBar alloc] init];
naviBar.items = [NSArray arrayWithObject:navItem];
naviBar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0);
[self.view addSubview:naviBar];
[navItem release];
FenchKiss Dev
That was it! I had no idea that I need to create UINavigationItem. I thought that UITableView has already one. Thanks @FenchKiss Dev!
Jakub