toolbar除了可以和navigationController一起用之外,也可以獨立用到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 //建立toolbar 24 UIToolbar *toolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 420.0f, 320.0f, 40.0f) ] autorelease]; 25 26 //建立barbuttonitem 27 UIBarButtonItem *item1 = [[[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStyleBordered target:self action:@selector(test:)] autorelease]; 28 29 //建立barbuttonitem 30 UIBarButtonItem *item2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil] autorelease]; 31 32 //建立一個segmentController 33 UISegmentedControl *seg = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"牛扒", @"排骨", nil] ] autorelease]; 34 35 //設定style 36 [seg setSegmentedControlStyle:UISegmentedControlSegmentCenter]; 37 38 39 [seg addTarget:self action:@selector(segmentControllerItem:) forControlEvents:UIControlEventValueChanged]; 40 41 //建立一個內容是view的uibarbuttonitem 42 UIBarButtonItem *itemSeg = [[[UIBarButtonItem alloc] initWithCustomView:seg] autorelease]; 43 44 //建立barbuttonitem,樣式是flexible,這個種barbuttonitem用於兩個barbuttonitem之間 45 //調整兩個item之間的距離.flexible表示距離是動態,fixed表示是固定的 46 UIBarButtonItem *flexible = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease]; 47 48 //把item添加到toolbar裡 49 [toolBar setItems:[NSArray arrayWithObjects:item1,flexible,itemSeg,flexible,item2, nil] animated:YES]; 50 51 //把toolbar添加到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:@"%@ 被選中了",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