In addition to being used together with navigationcontroller, toolbar can also be used independently in view
1 // 2 // twfxtoolbarviewcontroller. m 3 // demotoolbar 4 // 5 // created by lion user on 13-1-19. 6 // copyright (c) 2013 lion user. all rights reserved. 7 // 8 9 # import "twfxtoolbarviewcontroller. H "10 11 @ interface twfxtoolbarviewcontroller () 12 13 @ end 14 15 @ implementation twfxtoolbarviewcontroller 16 17-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil 18 {19 self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; 20 if (Self) {21 // custom initialization 22 23 // create a toolbar 24 toolbar * toolbar = [[uitoolbar alloc] initwithframe: cgrectmake (0.0f, 420366f, 320366f, 40366f)] autorelease]; 25 26 // create barbuttonitem 27 uibarbuttonitem * Item1 = [[[uibarbuttonitem alloc] initwithtitle: @ "" style: Custom target: Self action: @ selector (test :)] autorel.pdf]; 28 29 // create barbuttonitem 30 uibarbuttonitem * item2 = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitembookmarks target: Self action: Nil] autorelease]; 31 32 // create a segmentcontroller 33 uisegmentedcontrol * seg = [[uisegmentedcontrol alloc] initwithitems: [nsarray Plugin: @ "steak", @ "pork ribs", nil] autorelease]; 34 35 // set style 36 [seg setsegmentedcontrolstyle: uisegmentedcontrolsegmentcenter]; 37 38 39 [seg addtarget: Self action: @ selector (segmentcontrolleritem :) forcontrolevents: uicontroleventvaluechanged]; 40 41 // create a view uibarbuttonitem 42 uibarbuttonitem * itemseg = [[uibarbuttonitem alloc] parts: seg] autorelease]; 43 44 // create a barbuttonitem with the style flexible, this type of barbuttonitem is used for 45 between two barbuttonitems // adjust the distance between two items. flexible indicates that the distance is dynamic, and fixed indicates that the distance is a fixed 46 uibarbuttonitem * flexible = [[[uibarbuttonitem alloc] initwithbarbuttonsystemitem: Custom target: Self action: Nil] autorelease]; 47 48 // Add the item to the toolbar 49 [toolbar setitems: [nsarray arraywithobjects: Item1, flexible, itemseg, flexible, item2, nil] animated: Yes]; 50 51 // Add toolbar to view 52 [self. view addsubview: toolbar]; 53 54} 55 return self; 56} 57 58-(void) viewdidload 59 {60 [Super viewdidload]; 61 // do any additional setup after loading the view from Its nib. 62} 63 64-(void) test :( ID) sender 65 {66 uibarbuttonitem * Item = (uibarbuttonitem *) sender; 67 nsstring * Title = [nsstring stringwithformat: @ "% @ selected", item. title]; 68 69 uialertview * alertview = [[[uialertview alloc] initwithtitle: @ "Attention" message: Title delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: nil, nil] autorelease]; 70 71 [alertview show]; 72} 73 74 75-(void) segmentcontrolleritem :( ID) sender 76 {77 uisegmentedcontrol * seg = (uisegmentedcontrol *) sender; 78 nsinteger Index = seg. selectedsegmentindex; 79 nsstring * message; 80 If (Index = 0) {81 message = @ ""; 82} 83 else 84 {85 message = @ ""; 86} 87 88 uialertview * alertview = [[[uialertview alloc] initwithtitle: @ "attenton" message: message delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: nil, nil] autorelease]; 89 90 [alertview show]; 91} 92 93-(void) didreceivememorywarning 94 {95 [Super didreceivememorywarning]; 96 // dispose of any resources that can be recreated. 97} 98 99-(ibaction) Goback :( uibutton *) sender {100 101 [self dismissviewcontrolleranimated: Yes completion: Nil]; 102} 103 @ end