iPhone UITableView
I have an iPhone application that has a UITabBarController
that houses multiple UITableViews
. I need to be able to add items to each UITableView
(inside each tab), but my Google-fu is failing me so epically that I cannot find examples of how to put in the "+" button in the upper right corner, in the navigation item. This "+" button would open a subview to enter the new item's information, which I should be able to handle once I get the button to app开发者_如何学Goear. I'd like to do this programatically, not through IB. Can someone enlighten me?
You can set the button in the right of the Navigation bar like this:
self.navigationItem.rightBarButtonItem = button;
The button will need to be a UIBarButtonItem. You can create the UIBarButtonItem and add a target for when the button is pressed, like this:
button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(method:)];
The target and action (selector to call when it is pressed) can be changed to whatever you need.
Together, in one line, it looks like this:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(method:)];
This should be all that is necessary to create an add button, and add it to the navigationItem.
The previous answer, that talked about using a UIButton is below:
This is how you'd create a regular button, if necessary:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(addButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
That will call addButtonPressed in the class where you created the button, when it is pressed. You can change the target and selector to whatever you need.
Together:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(addButtonPressed)
forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = button;
Hope this helps!
set UIBarButtonItem to self.navigationIten.rightBarButtonItem and handle its action.
To add rows use insertRowAtIndexPath method of tableView.
精彩评论