iphone - How to add a UIToolbar to a UITableViewController programmatically? -
i have opted use uitableviewcontroller
without nib. need uitoolbar @ bottom 2 buttons. simplest way of doing that?
p.s. know can use uiviewcontroller
, add uitableview
want things consistent across app.
can help?
i saw following example , not sure on validity:
(void)viewwillappear:(bool)animated { [super viewwillappear:animated]; //initialize toolbar toolbar = [[uitoolbar alloc] init]; toolbar.barstyle = uibarstyledefault; //set toolbar fit width of app. [toolbar sizetofit]; //caclulate height of toolbar cgfloat toolbarheight = [toolbar frame].size.height; //get bounds of parent view cgrect rootviewbounds = self.parentviewcontroller.view.bounds; //get height of parent view. cgfloat rootviewheight = cgrectgetheight(rootviewbounds); //get width of parent view, cgfloat rootviewwidth = cgrectgetwidth(rootviewbounds); //create rectangle toolbar cgrect rectarea = cgrectmake(0, rootviewheight - toolbarheight, rootviewwidth, toolbarheight); //reposition , resize receiver [toolbar setframe:rectarea]; //create button uibarbuttonitem *infobutton = [[uibarbuttonitem alloc] initwithtitle:@"back" style:uibarbuttonitemstylebordered target:self action:@selector(info_clicked:)]; [toolbar setitems:[nsarray arraywithobjects:infobutton,nil]]; //add toolbar subview navigation controller. [self.navigationcontroller.view addsubview:toolbar]; [[self tableview] reloaddata]; } (void) info_clicked:(id)sender { [self.navigationcontroller popviewcontrolleranimated:yes]; [toolbar removefromsuperview]; }
the simpler thing build project on top of uinavigationcontroller
. has toolbar, it's hidden default. can reveal toggling toolbarhidden
property, , table view controller able use long it's in navigation controller hierarchy.
in app delegate, or in object app delegate passes control to, create navigation controller uitableviewcontroller
root view controller:
- ( void )application: (uiapplication *)application didfinishlaunchingwithoptions: (nsdictionary *)options { mytableviewcontroller *tableviewcontroller; uinavigationcontroller *navcontroller; tableviewcontroller = [[ mytableviewcontroller alloc ] initwithstyle: uitableviewstyleplain ]; navcontroller = [[ uinavigationcontroller alloc ] initwithrootviewcontroller: tableviewcontroller ]; [ tableviewcontroller release ]; /* ensure toolbar visible */ navcontroller.toolbarhidden = no; self.navigationcontroller = navcontroller; [ navcontroller release ]; [ self.window addsubview: self.navigationcontroller.view ]; [ self.window makekeyandvisible ]; }
then set toolbar items in mytableviewcontroller
object:
- ( void )viewdidload { uibarbuttonitem *buttonitem; buttonitem = [[ uibarbuttonitem alloc ] initwithtitle: @"back" style: uibarbuttonitemstylebordered target: self action: @selector( goback: ) ]; self.toolbaritems = [ nsarray arraywithobject: buttonitem ]; [ buttonitem release ]; /* ... additional setup ... */ }
Comments
Post a Comment