无涯教程-OC - Toolbar函数

55 阅读1分钟

如果无涯教程想基于当前视图来操作某些东西,可以使用工具栏。

如带有收件箱项目的电子邮件应用程序,该项目具有删除,收藏,回复等选项。如下所示。

iOS Tutorial

Toolbar - 重要属性

  • barStyle
  • items

Toolbar - 自定义方法

-(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: 
   customItem1spaceItem customItem2 nil];
   UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:
   CGRectMake(0 366+54 320 50)];
   [toolbar setBarStyle:UIBarStyleBlackOpaque];
   [self.view addSubview:toolbar];
   [toolbar setItems:toolbarItems];
}

为了知道执行的操作,无涯教程在 ViewController.xib 中添加 UILabel ,并为UILabel创建 IBoutlet ,并将其命名为标签。

无涯教程还需要添加两种方法来执行工具栏项的操作,如下所示。

-(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];

//2 秒后调用的方法 hideStatusbar [self addToolbar]; //Do any additional setup after loading the view, typically from a nib. }

运行应用程序时,将获得以下输出-

iOS Tutorial

单击工具1和工具2的条形按钮,无涯教程得到以下内容-

iOS Tutorial

参考链接

www.learnfk.com/ios/ios-ui-…