UI 07 _ 导航视图控制器 与 属性传值
Posted brucemengbm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UI 07 _ 导航视图控制器 与 属性传值相关的知识,希望对你有一定的参考价值。
首先, 先创建三个VC.
完毕点击按钮, 进入下一页, 并可以返回.
要先把导航视图控制器创建出来.
在AppDelegate.m 文件里代码例如以下:
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
//先创建一个ViewController
MainViewController *mainVC = [[MainViewController alloc] init];
//创建导航视图控制器
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navC;
//释放
[mainVC release];
[navC release];
return YES;
}
对于导航视图控制器的一些设置.
//导航视图控制器的高度是44,上面的状态栏高度是20,加在一起默认是64;
// 加上一个标题.
self.title = @"猫眼电影";
// 对外观进行设置,不是全部的颜色都是BackgroundColor;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
// 创建一个UIView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
[view release];
// 为了防止坐标系被篡改,我们把bar从半透明改为全透明.这样坐标系的原点会自己主动向下推64
self.navigationController.navigationBar.translucent = NO;
内容方面的设置
//另外一种标题的设置
self.navigationItem.title = @"骨头 商店";
// 指定一些视图,作为titleView
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"信息",@"通话"]];
self.navigationItem.titleView = seg;
[seg release];
创建左右两个按钮
//左按钮
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemOrganize target:self action:@selector(leftAction:)] autorelease];
//右按钮
//如此创建,原本拖拽的图标的颜色是黄色,但这么创建后,图标是蓝色的.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"狗.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
// 以下方法图标将会显示黄色.
// 创建一个小button,把图片装上.
UIButton *Littlebutton = [UIButton buttonWithType:UIButtonTypeCustom];
Littlebutton.frame = CGRectMake(0, 0, 40, 40);
[Littlebutton setImage:[UIImage imageNamed:@"狗.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Littlebutton];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(260, 530, 80, 40);
button.backgroundColor = [UIColor yellowColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
button.layer.cornerRadius = 10;
[button setTitle:@"下一页" forState:UIControlStateNormal];
对应的, 左右两个按钮的点击事件也要实现
- (void)rightAction:(UIBarButtonItem *)button{
}
- (void)leftAction:(UIBarButtonItem *)barButton{
}
点击Button跳转到下一页.
用模态也可以跳转 , 可是跳转的页面不再有导航视图.
实现Button的点击事件
当中我要完毕从前向后传值.
- (void)click:(UIButton *)button{
- // 用导航视图控制器跳转.
//1.先创建下一页对象.
SecondViewController *secondVC = [[SecondViewController alloc] init];
//通过导航控制器找到
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
// 属性传值第二步
secondVC.number = 100;
secondVC.string = self.textField.text;
secondVC.arr = @[@"haha",@"啦啦"];
}
第二个页面的 .h 中代码:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
// 属性传值第一步,在第二个页面写一条属性.
@property(nonatomic, assign)NSInteger number;
//属性传值另外一种: 针对TextField中的字符串写一条属性.
@property(nonatomic, copy)NSString *string;
//属性传值第三种: 传一个数组
@property(nonatomic, retain)NSArray *arr;
@end
第二个页面的 .m 中代码:
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@property(nonatomic, retain)UILabel *label;
@end
@implementation SecondViewController
- (void)dealloc{
[_label release];
[_string release];
[_arr release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
self.navigationController.navigationBar.barTintColor = [UIColor magentaColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(260, 530, 80, 40);
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
button.layer.cornerRadius = 10;
[button setTitle:@"下一页" forState:UIControlStateNormal];
//第三步:
//第一种:
NSLog(@"%ld",self.number);
//另外一种:
self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
[self.view addSubview:self.label];
[_label release];
self.label.layer.borderWidth = 1;
//属性传值第三步:里面的值赋给label
self.label.text = self.string;
// 第三种:
NSLog(@"%@",self.arr[1]);
}
- (void)click:(UIButton *)button{
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdVC animated:YES];
[thirdVC release];
}
属性传值. 传NSIntger . NSString . NSArray
以上是关于UI 07 _ 导航视图控制器 与 属性传值的主要内容,如果未能解决你的问题,请参考以下文章
UI 多个视图控制器(UIViewController)间的 协议传值