iOS 教程
我們可以使用工具欄修改視圖元素。
如,郵件應(yīng)用程序里的收件箱欄中有刪除、分享、答復(fù)等等。如下所示:
重要的屬性
-(void)addToolbar { UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc] initWithTitle:@"Tool1" style:UIBarButtonItemStyleBordered target:self action:@selector(toolBarItem1:)]; UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc] initWithTitle:@"Tool2" style:UIBarButtonItemStyleDone target:self action:@selector(toolBarItem2:)]; NSArray *toolbarItems = [NSArray arrayWithObjects: customItem1,spaceItem, customItem2, nil]; UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame: CGRectMake(0, 366+54, 320, 50)]; [toolbar setBarStyle:UIBarStyleBlackOpaque]; [self.view addSubview:toolbar]; [toolbar setItems:toolbarItems]; }
為了解所執(zhí)行的操作我們?cè)谖覀兊腣iewController.xib中添加UILabel Iboutlet并為?UILabel?創(chuàng)建命名為標(biāo)簽的IBoutlet。
我們還需要添加兩個(gè)方法來執(zhí)行,如下所示的工具欄項(xiàng)的操作:
-(IBAction)toolBarItem1:(id)sender{ [label setText:@"Tool 1 Selected"]; } -(IBAction)toolBarItem2:(id)sender{ [label setText:@"Tool 2 Selected"]; }
在ViewController.m中更新?viewDidLoad,如下所示:
- (void)viewDidLoad { [super viewDidLoad]; // The method hideStatusbar called after 2 seconds [self addToolbar]; // Do any additional setup after loading the view, typically from a nib. }
現(xiàn)在當(dāng)我們運(yùn)行該應(yīng)用程序我們會(huì)看到下面的輸出。
單擊我們得到的?tool1?和?tool2?欄按鈕