IPhone之navigationItem

本文介绍如何在iPhone应用中使用UINavigationController来定制导航栏,包括添加按钮、集成segmentedControl、设置标题及返回按钮等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IPhone之navigationItem

注:

使用其navigationItem时,首先要确保UINavigationController 视图已经加载到ViewController中。

其代码为:

 

[[UINavigationController alloc] initWithRootViewController:viewController];

 

 

1、添加按钮

 

UIBarButtonItem *loginBut =[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStylePlain target:self action:@selector(login)];   
    self.navigationItem.rightBarButtonItem = loginBut;   
    [loginBut release];
   
    UIBarButtonItem *createBut =[[UIBarButtonItem alloc] initWithTitle:@"NEW" style:UIBarButtonItemStylePlain target:self action:@selector(creatUser)];   
    self.navigationItem.leftBarButtonItem = createBut;   
    [createBut release];

 

 

2、添加segmentedControl

 

UISegmentedControl *m_segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:
                                            [UIImage imageNamed:@"up.png"],
                                            [UIImage imageNamed:@"down.png"],
                                            nil]];
    [m_segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    m_segmentedControl.frame = CGRectMake(0, 0, 90, 30);
    m_segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    m_segmentedControl.momentary = YES;
   
    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:m_segmentedControl];
    [m_segmentedControl release];
   
    self.navigationItem.rightBarButtonItem = segmentBarItem;
    [segmentBarItem release];

 

 

3、设置标题

 

 self.navigationItem.title = @"详细内容";

 

 

4、设置返回按钮

 

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"主菜单" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值