iOS应用开发中导航栏按钮UIBarButtonItem的添加教程

这篇文章主要介绍了iOS应用开发中导航栏按钮UIBarButtonItem的添加教程,文中详细介绍了使用UINavigationController导航控制器添加的过程,需要的朋友可以参考下

1、UINavigationController导航控制器如何使用
UINavigationController可以翻译为导航控制器,在iOS里经常用到。
我们看看它的如何使用:
下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制

20162494526157.png-600 (636×288)

2、UINavigationController的结构组成
看下图,UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等组成。

20162494548655.png-600 (646×476)

现在我们建立一个例子,看看如何使用UINavigationController
3、新建一个项目
命名为UINavigationControllerDemo,为了更好理解UINavigationController,我们选择Empty Application模板

20162494608179.png-600 (728×491)

4、创建一个View Controller,命名为RootViewController:依次选择File――New――New File,默认勾上With XIB for user interface.

20162494625133.png-600 (728×491)

选择正确位置创建完成,这时项目里多了三个文件,分别是RootViewController.h RootViewController.m RootViewController.xib文件。
打开RootViewController.xib,添加一个按钮控件,按钮Button改成 :Goto SecondView,为跳转做准备

20162494706858.png-600 (923×284)

5、打开AppDelegate.h,向其中添加属性:

复制代码 代码如下:

@property (strong, nonatomic) UINavigationController *navController; 

添加后AppDelegate.h文件代码如下:
复制代码 代码如下:

#import  
 
@class ViewController; 
 
@interface AppDelegate : UIResponder  
 
@property (strong, nonatomic) UIWindow *window; 
 
@property (strong, nonatomic) ViewController *viewController; 
 
@property (strong, nonatomic) UINavigationController *navController; 
 
@end 

6、在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中创建添加navController,RootViewController视图。
复制代码 代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    RootViewController *rootView = [[RootViewController alloc] init]; 
    rootView.title = @"Root View"; 
     
    self.navController = [[UINavigationController alloc] init]; 
    [self.navController pushViewController:rootView animated:YES]; 
    [self.window addSubview:self.navController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 


给rootView的titie命名为 Root View,好识别View直接的切换关系。用pushViewController把rootView加入到navController的视图栈中。
7、现在Root视图添加完成
看看效果:

20162494734438.png-600 (368×716)

现在还没有Navigation bar 。只有title。
8、添加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;

}


这样添加了UIBarButtonItem了,效果如下:

20162494751281.png-600 (362×214)

这里重点介绍下

复制代码 代码如下:

UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];

以上就是iOS应用开发中导航栏按钮UIBarButtonItem的添加教程的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动