1.添加UIBarButtonItem
bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。
在RootViewController.m中添加代码如下:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
- self.navigationItem.leftBarButtonItem = leftButton;
- UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];
- self.navigationItem.rightBarButtonItem = rightButton;<p class="p1">}</p>
****这里重点介绍下
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];
UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格,看下图,你不用一个个试验,你也知道想用那个item,如下图:
2.响应UIBarButtonItem的事件的实现
我们在 action:@selector(selectLeftAction:);
action添加了selectLeftAction和selectRightAction
在RootViewController.m文件中添加代码实现:
- -(void)selectLeftAction:(id)sender
- {
- UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
- [alter show];
- }
- -(void)selectRightAction:(id)sender
- {
- UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏右按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
- [alter show];
- }
1.UIBbarButtonItem有如下几种初始化的方法:
-initWithTitle
-initWithImage
-initWithBarButtonSystemI
-initWithCustomView
第4种方法就是我们添加各种作料的接口,所以今天的主角其它也是它。
2.在UIToolBar上面添加Title
- UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:
- CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
- NSMutableArray *myToolBarItems = [NSMutableArray array];
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithTitle:@"myTile"
- style:UIBarButtonItemStylePlai
n - target:self
- action:@selector(action)] autorelease]];
- [myToolBar setItems:myToolBarItems animated:YES];
- [myToolBar release];
- [myToolBarItems];
setItems传入值或者说items是一个对象数组。
3.在UIToolBar上面添加image
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithImage:[UIImage imageNamed:@"myImage.png"]
- style:UIBarButtonItemStylePlai
n - target:self
- action:@selector(action)]];
4.在UIToolBar上面添加SystemItem
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithBarButtonSystemI
tem:UIBarButtonSystemItemPla y - target:self
- action:@selector(action)] autorelease]];
Note:
initWithBarButtonSystemI
- (id)initWithBarButtonSystemI
Defines system defaults for commonly used items.
- typedef enum {
- UIBarButtonSystemItemDon
e, - UIBarButtonSystemItemCan
cel, - UIBarButtonSystemItemEdi
t, - UIBarButtonSystemItemSav
e, - UIBarButtonSystemItemAdd
, - UIBarButtonSystemItemFle
xibleSpace, - UIBarButtonSystemItemFix
edSpace, - UIBarButtonSystemItemCom
pose, - UIBarButtonSystemItemRep
ly, - UIBarButtonSystemItemAct
ion, - UIBarButtonSystemItemOrg
anize, - UIBarButtonSystemItemBoo
kmarks, - UIBarButtonSystemItemSea
rch, - UIBarButtonSystemItemRef
resh, - UIBarButtonSystemItemSto
p, - UIBarButtonSystemItemCam
era, - UIBarButtonSystemItemTra
sh, - UIBarButtonSystemItemPla
y, - UIBarButtonSystemItemPau
se, - UIBarButtonSystemItemRew
ind, - UIBarButtonSystemItemFas
tForward, - UIBarButtonSystemItemUnd
o, // iPhoneOS 3.0 - UIBarButtonSystemItemRed
o, // iPhoneOS 3.0 - } UIBarButtonSystemItem;
5.在UIToolBar上面添加其它各种控件,使用initWithCustomView来完成.
这里需要看一下initWithCustomView的定义:
- (id)initWithCustomView:(UIView *)customView
可以看出,它的参数是一个view
A>加一个开关switch:
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithCustomView:[[[UISwitch alloc] init] autorelease]]
- autorelease]];
B>加一个按钮UIBarButtonItem
- UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]
- initWithTitle:@"myButton"
- style:UIBarButtonItemStyleBord
ered - target:self
- action:@selector(action)]autorelease];
- get1Button.width = 50;
- [myToolBarItems addObject:myButton];
C>加一个文本Label
- UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)];
- myLabel.font=[UIFont systemFontOfSize:10];
- //myLabel.backgroundColor = [UIColor clearColor];
- //myLabel.textAlignment=UITextAlignmentCenter;
- UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myLabel];
- [myToolBarItems addObject: myButtonItem];
- [mylabel release];
- [myButtonItem release];
D>加一个进度条UIProgressView
- UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)];
- UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myProgress];
- [myToolBarItems addObject: myButtonItem];
- [myProgress release];
- [myButtonItem release];
6,代码示例
4.17UIToolBar
- (void)viewDidLoad
{
#if 0
#else
//UIToolbar中加入的按钮都是UIBarButtonItem类型
#endif
}