toolbar除了可以和navigationController一起用之外,也可以独立用到view里。工具栏UIToolbar – 一般显示在底部,用于提供一组选项,让用户执行一些功能,而并非用于在完全不同的UI界面之间切换;在不同 UI 界面切换,是选项卡 Tab Bar负责的。

  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

ios之UIToolBar的更多相关文章

  1. iOS中 UIToolBar 技术分享

    UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏.当设置UIToolBar显示,或者存在UITabBarController且tabbar被隐藏的时候 ...

  2. iOS - UIToolbar

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIToolbar : UIView <UIBarPositioning& ...

  3. ios 开发UI篇— UIToolbar

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIToolbar : UIView <UIBarPositioning& ...

  4. iOS 开发 中级:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem自定义方法总结

    原文:  http://blog.csdn.net/songrotek/article/details/8692866?utm_source=tuicool 对于UIToolbar,UINavigat ...

  5. iOS开发——UI篇Swift篇&UIToolbar

    UIToolbar class UIToolBarUISearchBar: UIViewController,UISearchBarDelegate { var titleString:String! ...

  6. [iOS基础控件 - 6.10.3] DatePicker & UIToolBar

    A.需求 1. 学习DatePicker的基本配置 2.使用TextField召唤指定类型的输入键盘View,这里使用DatePicker 3.给输入键盘上方加上一个UIToolBar,实现如关闭键盘 ...

  7. 【iOS发展-70】点菜系统案例:使用文本框inputView和inputAccessoryView串联UIPickerView、UIDatePicker和UIToolBar

    (1)效果 (2)先在storyboard中设计界面,然后源码(直接在ViewController中码) #import "ViewController.h" @interface ...

  8. 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )

    转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...

  9. iOS:UIToolBar控件的使用

    UIToolBar控件:是经常使用的一个工具条控件,虽然在上面可以添加子控件,但是toolbar中只能添加UIBarButtonItem类型的子控件,其他子控件会被包装成这种类型的,例如UIButto ...

随机推荐

  1. P4827 [国家集训队] Crash 的文明世界(第二类斯特林数+树形dp)

    传送门 对于点\(u\),所求为\[\sum_{i=1}^ndis(i,u)^k\] 把后面那堆东西化成第二类斯特林数,有\[\sum_{i=1}^n\sum_{j=0}^kS(k,j)\times ...

  2. [Xcode 实际操作]七、文件与数据-(16)解析XML文档

    目录:[Swift]Xcode实际操作 本文将演示如何解析XML文档. 项目中已添加一份XML文档:worker.xml <?xml version="1.0" encodi ...

  3. web框架原理,http 协议

    目录 web框架原理 web框架是什么东西 执行代码用浏览器访问一下 输出结果 http 协议 http 协议简介 http 协议概述 http 工作原理 http请求方法 http 状态码 url介 ...

  4. SpringBoot整合Memached

    一.Memached介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站 ...

  5. Nginx 405 not allowed最简单快速解决办法

    Apache.IIS.Nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否则会返回“HTTP/1.1 405 Method not allowed”错误. server { list ...

  6. Hungary Algorithm国外板子

    Codeforces 1107一题除了dp做法还有二分带权匹配O(n^3)做法,国外网友的板子时间非常优秀,但矩阵设定的事情并不是很懂-- //Codeforces 1107F const int m ...

  7. python学习之邮件

    分类smtp邮件 html邮件,带附件的邮件. 一 STTP邮件:设置SMTP代理机构,发送人的邮箱和密码,收件人的邮箱地址(email模块):接收端,发送(smtplib模块).SMTP(smtp_ ...

  8. 求N之下的所有素数

    No.1 f=lambda n: [x for x in range(1,n) if not [y for y in range(2,x) if x%y ==0 ]] No.2 def prime(n ...

  9. SQL Server 查看列,添加列,修改列,删除列

    查看表:exec sp_help 表名 查看列: exec sp_columns 表名 查看列:select * from information_schema.columns where table ...

  10. Nginx 开启多核cpu配置

    nginx默认是没有开启利用多核cpu的配置的.需要通过增加worker_cpu_affinity配置参数来充分利用多核cpu; 需要在nginx配置里添加 worker_processes 和 or ...