UINavgationBar之UIBarButtonItem

创建一个导航按钮,须要做一下工作:

创建一个 UIBarButtonItem 类实例。

使用视图控件的NavigationItem 属性给导航栏添加按钮。NavigationItem 属性允许我们设置这个导航栏。这个属性自身有两个属性,分别为 rightBarButtonItem 和 leftBarButtonItem。这两个属性都属于 UIBarButtonItem 类。

下面是一个为导航栏右侧添加按钮的例子:

- (void)performAdd:(id)paramSender{

 NSLog(@"Action method got called.");

 }

-(void)viewDidLoad{

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

self.title = @"First Controller";

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Add" style:UIBarButtonItemStylePlain

target:self

action:@selector(performAdd:)];

}

结果如下:

这里面style的参数是UIBarButtonSystemItem枚举类型中的任意一个,如下所示:

typedef enum {

UIBarButtonSystemItemDone,

UIBarButtonSystemItemCancel,

UIBarButtonSystemItemEdit,

UIBarButtonSystemItemSave,

UIBarButtonSystemItemAdd,

UIBarButtonSystemItemFlexibleSpace,

UIBarButtonSystemItemFixedSpace,

UIBarButtonSystemItemCompose,

UIBarButtonSystemItemReply,

UIBarButtonSystemItemAction,UIBarButtonSystemItemOrganize,

UIBarButtonSystemItemBookmarks,UIBarButtonSystemItemSearch,

UIBarButtonSystemItemRefresh,UIBarButtonSystemItemStop,

UIBarButtonSystemItemCamera,UIBarButtonSystemItemTrash,

UIBarButtonSystemItemPlay,UIBarButtonSystemItemPause,

UIBarButtonSystemItemRewind,

UIBarButtonSystemItemFastForward,

UIBarButtonSystemItemUndo,

UIBarButtonSystemItemRedo,

UIBarButtonSystemItemPageCurl,

} UIBarButtonSystemItem;

类 UIBarButtonItem 中的真正最大的初始化方法中,其中一个是 initWithCustomView:方法.这里接收的参数是任何视图类.

下面举个例子:

- (void) switchIsChanged:(UISwitch *)paramSender{

if ([paramSender isOn]){
NSLog(@"Switch ison.");
}

else {NSLog(@"Switch is off.");
}


}


- (void)viewDidLoad{


[super viewDidLoad];


self.view.backgroundColor = [UIColor whiteColor];

self.title = @"First Controller";

UISwitch *simpleSwitch = [[UISwitch alloc] init];

simpleSwitch.on = YES;


[simpleSwitch addTarget:selfaction:@selector(switchIsChanged:)forControlEvents:UIControlEventValueChanged];self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc]initWithCustomView:simpleSwitch];

}

运行结果如下:

                           

ok,最后我们做个稍微复杂一点的:

- (void) segmentedControlTapped:(UISegmentedControl *)paramSender{

if ([paramSender selectedSegmentIndex] == 0){

/* Up button */
NSLog(@"Up");


}

else if ([paramSender selectedSegmentIndex] == 1){

/* Down button */ NSLog(@"Down");

}


}

-       (void)viewDidLoad{


[super viewDidLoad];


self.view.backgroundColor = [UIColor whiteColor];


self.title = @"First Controller";


NSArray *items = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"UpArrow.png"],


                              [UIImage imageNamed:@"DownArrow.png"], nil];


UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];


segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

segmentedControl.momentary = YES;


[segmentedControl addTarget:self action:@selector(segmentedControlTapped:)

                forControlEvents:UIControlEventValueChanged];

self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:

segmentedControl];


}

 

 

同样,如果是个自定义的按钮也可以使用类似上面的方法.

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值