iOS导航栏创建

本文是使用纯代码实现一个导航栏的效果。单击按钮并且产生事件。基本思路是:

1.创建一个导航栏(UINavigationBar对象)

2.创建一个导航栏集合(UINavigationItem对象)

3.创建一个左边按钮、一个右边按钮(UIBarButtonItem对象),并实现对应的事件方法

4.将导航栏集合添加到导航栏中,设置动画关闭

5.把左右两个按钮添加到导航栏集合中去

6.在视图中显示当前创建的导航栏

 


具体的实现代码如下:

ViewController.h文件中的代码不用改变,如下所示:

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5. @end  
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m文件中的代码:

  1. #import “ViewController.h”  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.     // Do any additional setup after loading the view, typically from a nib.  
  13.       
  14.     //创建一个导航栏  
  15.     UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
  16.     //创建一个导航栏集合  
  17.     UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];  
  18.     //在这个集合Item中添加标题,按钮  
  19.     //style:设置按钮的风格,一共有三种选择  
  20.     //action:@selector:设置按钮的点击事件  
  21.     //创建一个左边按钮  
  22.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@”左边” style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)];  
  23.     //创建一个右边按钮  
  24.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@”右边” style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];  
  25.       
  26.     //设置导航栏的内容  
  27.     [navItem setTitle:@”凌凌漆”];  
  28.       
  29.     //把导航栏集合添加到导航栏中,设置动画关闭  
  30.     [navBar pushNavigationItem:navItem animated:NO];  
  31.       
  32.     //把左右两个按钮添加到导航栏集合中去  
  33.     [navItem setLeftBarButtonItem:leftButton];  
  34.     [navItem setRightBarButtonItem:rightButton];  
  35.       
  36.     //将标题栏中的内容全部添加到主视图当中  
  37.     [self.view addSubview:navBar];  
  38.       
  39.     //最后将控件在内存中释放掉,以避免内存泄露  
  40.     [navItem release];  
  41.     [leftButton release];  
  42.     [rightButton release];  
  43. }  
  44.   
  45. -(void)showDialog:(NSString *)str  
  46. {  
  47.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”这是一个对话框” message:str delegate:self cancelButtonTitle:@“确定” otherButtonTitles: nil];  
  48.     [alert show];  
  49.     [alert release];  
  50. }  
  51.   
  52. -(void) clickLeftButton  
  53. {  
  54.     [self showDialog:@”点击了导航栏左边按钮”];  
  55. }  
  56.   
  57. -(void) clickRightButton  
  58. {  
  59.     [self showDialog:@”点击了导航栏右边按钮”];  
  60. }  
  61.   
  62. - (void)viewDidUnload  
  63. {  
  64.     [super viewDidUnload];  
  65.     // Release any retained subviews of the main view.  
  66. }  
  67.   
  68. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  69. {  
  70.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  71. }  
  72.   
  73. @end  
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //创建一个导航栏
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    //创建一个导航栏集合
    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];
    //在这个集合Item中添加标题,按钮
    //style:设置按钮的风格,一共有三种选择
    //action:@selector:设置按钮的点击事件
    //创建一个左边按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)];
    //创建一个右边按钮
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];

    //设置导航栏的内容
    [navItem setTitle:@"凌凌漆"];

    //把导航栏集合添加到导航栏中,设置动画关闭
    [navBar pushNavigationItem:navItem animated:NO];

    //把左右两个按钮添加到导航栏集合中去
    [navItem setLeftBarButtonItem:leftButton];
    [navItem setRightBarButtonItem:rightButton];

    //将标题栏中的内容全部添加到主视图当中
    [self.view addSubview:navBar];

    //最后将控件在内存中释放掉,以避免内存泄露
    [navItem release];
    [leftButton release];
    [rightButton release];
}

-(void)showDialog:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
    [alert release];
}

-(void) clickLeftButton
{
    [self showDialog:@"点击了导航栏左边按钮"];
}

-(void) clickRightButton
{
    [self showDialog:@"点击了导航栏右边按钮"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中,我们可以使用Action Extension来扩展应用程序的功能。Action Extension是一种允许用户在其他应用程序中执行特定操作的扩展,例如分享内容、保存信息等。 要创建Action Extension扩展,可以按照以下步骤进行操作: 1. 在Xcode中打开你的应用程序项目。 2. 在左侧导航栏中选择项目的名称,打开项目设置。 3. 在顶部菜单栏中选择"Target"。 4. 点击页面下方的"+"按钮,选择"Action Extension"作为新的目标。 5. 输入Extension的名称,例如"ShareExtension",然后点击"Finish"。 6. Xcode会自动生成一些默认的文件和设置,你可以根据需求进行修改。 7. 打开"Info.plist"文件,配置Action Extension的一些基本信息,例如名称、图标等。 8. 在Action Extension的代码文件中,你可以获取分享的内容,并进行相应的处理。 9. 你还可以在Action Extension中添加自定义的界面,使用户能够更方便地进行操作。 10. 编译并运行你的应用程序,可以选择其他应用程序中的内容,然后在分享菜单中找到你的Action Extension,执行相应的操作。 需要注意的是,为了使Action Extension正常工作,还需要在应用程序设置中配置一些权限和共享数据的支持。你可以在"Capabilities"选项卡中设置对应的权限和所需的共享数据类型。 创建完Action Extension后,你可以将其与应用程序打包在一起分发,用户可以通过在其他应用程序中选择分享菜单来使用你的Action Extension。 总的来说,创建iOS中的Action Extension扩展是一个相对简单的过程,通过使用Action Extension,你可以方便地为你的应用程序提供更多的功能和交互方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值