手机
当前位置:查字典教程网 >编程开发 >IOS开发 >iOS App开发中导航栏的创建及基本属性设置教程
iOS App开发中导航栏的创建及基本属性设置教程
摘要:文件目录如下:基本导航顺序:root->First->Second->Third。其中,FirstViewController作为navig...

文件目录如下:基本导航顺序: root -> First -> Second -> Third。其中,FirstViewController作为 navigation堆栈的rootview

iOS App开发中导航栏的创建及基本属性设置教程1

1、创建navigation

如果是想直接把navigation导航作为项目一开始的跟视图,把RootViewController.h文件里的nav属性放到AppDelegate.h里即可,再把RootViewController.m文件里的action的代码复制到 AppDelegate.m里的didFinishLaunchingWithOptions 方法里,最后把 self.window.rootViewController 设置 UINavigationController类型的属性nav即可

在RootViewController.h文件

复制代码 代码如下:

#import <UIKit/UIKit.h>

@class FirstViewController;

@interface RootViewController : UIViewController

@property (strong, nonatomic) UINavigationController *nav;

- (IBAction)btnClick:(UIButton *)sender;

@end

在RootViewController.m 文件里的随意一个自定义action里:

复制代码 代码如下:

- (IBAction)btnClick:(UIButton *)sender {

//创建一个viewcontroller

FirstViewController *fristview =[[[FirstViewController alloc] init] autorelease];

//初始化UINavigationController(方式一)

self.nav = [[[UINavigationController alloc] initWithRootViewController:fristview] autorelease];

//初始化UINavigationController(方式二)

// self.nav = [[[UINavigationController alloc] init] autorelease];

// [self.nav pushViewController:fristview animated:YES];

//初始化UINavigationController(方式三,失败,xib文件加载失败,原因暂时不明)

// self.nav = [[[UINavigationController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

//跳转到FirstView页面

[self presentViewController:self.nav animated:YES completion:nil];

//这种写法一般用于往view里添加一些小控件,如button label textField之类的,不适宜用于页面跳转

// [self.view addSubview:self.nav.view];

}

2.navigation的常用属性设置例子

我们的navigation就加载上去了以后,下面我们来设置navigation的属性:

复制代码 代码如下:

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

[self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明

self.title = @"navigationcontroller";//设置navigationbar上显示的标题

[self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:Nil];//设置navigationbar左边按钮

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:Nil];//设置navigationbar右边按钮

[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色

}

效果图如下:

iOS App开发中导航栏的创建及基本属性设置教程2

这里还有一个属性常用,就是:

复制代码 代码如下:

NSArray *arr = [NSArray arrayWithObjects:@"1",@"2", nil nil];

UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];

self.navigationItem.titleView = segment;//设置navigation上的titleview

效果如下:

iOS App开发中导航栏的创建及基本属性设置教程3

对,我们看到中间的字变成了两个可选的按钮,这就是navigation的另一个属性:navigationitem.titleview。

下面我们再建立一个视图,看一下两个视图之前是怎样通信的。

在第二个视图中,我添加了一个button来显示,并加了一个成员变量来接收从第一个视图中穿过来的值:

复制代码 代码如下:

@interface SecondViewController : UIViewController

@property (copy,nonatomic) NSString *str;

@end

复制代码 代码如下:

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

self.title = @"second";

UIButton *aBUTTON = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 50, 30)];

[aBUTTON setTitle:_str forState:UIControlStateNormal];

[aBUTTON addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:aBUTTON];

}

然后我将第一个视图的右边按钮添加一个事件,点击按钮,就会推出第二个视图,并显示我们传过来的值:

复制代码 代码如下:

- (void)clicked{

SecondViewController *second = [[SecondViewController alloc]init];

[self.navigationController pushViewController:second animated:YES];

second.str = @"hello!!";

[second release];

}

下面,我们来运行一下:

iOS App开发中导航栏的创建及基本属性设置教程4

点进按钮以后,我们的第二个视图推出,button显示了传过来的值。

然后我们点击回button,还有navigation另外一个方法:

复制代码 代码如下:

- (void)clicked{

[self.navigationController popViewControllerAnimated:YES];

}

这样就可以回到第一个视图。

【iOS App开发中导航栏的创建及基本属性设置教程】相关文章:

iOS的UI开发中UITabBarControlle的基本使用教程

iOS开发中对于摄像头的一些基本使用方法分享

iOS的UI开发中Button的基本编写方法讲解

iOS开发中最基本的位置功能实现示例

iOS开发中导航控制器的基本使用教程

iOS应用开发中视图控件UIWindow的基本使用教程

iOS开发中文件的上传和下载功能的基本实现

iOS App中数据管理框架Core Data的基本数据操作教程

iOS开发的UI制作中动态和静态单元格的基本使用教程

iOS开发中Quartz2D的基本使用方式举例

精品推荐
分类导航