导航栏/界面传值

self.view.backgroundColor = [UIColor redColor];

// 导航栏设置: controller(栏)/item(栏上的元素)

// 导航栏显示/隐藏

self.navigationController.navigationBarHidden = NO;

//    self.navigationController.navigationBar.hidden = YES;

// 栏样式

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

// 半透明效果

// 开始效果时 屏幕左上角为坐标原点

// 关闭时 导航栏的左下角为坐标原点

self.navigationController.navigationBar.translucent = YES;

// 创建view(0,0,100,100)

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

view.backgroundColor = [UIColor greenColor];

[self.view addSubview:view];

[view release];

// 栏背景颜色

self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];

// 栏颜色

self.navigationController.navigationBar.barTintColor = [UIColor grayColor];

// 栏标题

self.title = @"这是一个标题";

//    self.navigationItem.title = @"这是一个猴赛雷的标题";

// 分段

UISegmentedControl *seg = [[[UISegmentedControl alloc] initWithItems:@[@"消息", @"电话"]] autorelease];

seg.frame = CGRectMake(0, 0, 100, 30);

// 栏标题视图

self.navigationItem.titleView = seg;

// 栏左侧按钮

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(left:)] autorelease];

// 栏右侧按钮

// 系统按钮样式

UIBarButtonItem *b1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(right1)] autorelease];

// 自定义按钮图片

UIBarButtonItem *b2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"dianzan"] style:UIBarButtonItemStylePlain target:self action:@selector(right2)] autorelease];

self.navigationItem.rightBarButtonItems = @[b1, b2];

// 修改导航栏上内容的颜色

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

// 跳转页面

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.frame = CGRectMake(200, 200, 100, 100);

btn.backgroundColor = [UIColor yellowColor];

[self.view addSubview:btn];

[btn addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - 跳转页面

- (void)goTwo

{

// 1.获取第二页对象

TwoViewController *twoVC = [[TwoViewController alloc] init];

// 2.跳转(由导航控制器 从当前push到第二页)

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

// 3.内存管理

[twoVC release];

}

- (void)right1

{

NSLog(@"右1");

}

- (void)right2

{

NSLog(@"右2");

}

#pragma mark - 左按钮触发方法

- (void)left:(UIBarButtonItem *)left

{

NSLog(@"左点点");

}


界面间 传值

#import"RootViewController.h"

#import"TwoViewController.h"

#warning协议4:签协议

@interfaceRootViewController ()

@property(nonatomic,retain)UITextField*table;

@end

@implementationRootViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor= [UIColorwhiteColor];

self.navigationController.navigationBarHidden=NO;

self.navigationController.navigationBar.translucent=NO;

// self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

self.title=@"首页";

self.table= [[UITextFieldalloc]initWithFrame:CGRectMake(60,30,260,40)];

self.table.backgroundColor= [UIColorwhiteColor];

[self.viewaddSubview:self.table];

[self.tablerelease];

self.table.layer.borderColor= [UIColorlightGrayColor].CGColor;

self.table.layer.borderWidth=1;

self.table.layer.cornerRadius=5;

UIButton*btn = [UIButtonbuttonWithType:UIButtonTypeSystem];

btn.frame=CGRectMake(60,120,260,40);

btn.backgroundColor= [UIColorredColor];

[self.viewaddSubview:btn];

[btn addTarget:selfaction:@selector(goFor) forControlEvents:UIControlEventTouchUpInside];

}

-(void)goFor{

TwoViewController*two = [[TwoViewControlleralloc]init];

#warning属性2:在push页面之前传值(创建对象之后push之前)

two.string=self.table.text;

#warning协议5:设置代理人

//为了保证设置代理人的对象和设置push对象是同一个在创建对象之后push之前设置delegate

two.delegate=self;

[self.navigationControllerpushViewController:twoanimated:YES];

[tworelease];

}

#warning协议6:实现协议方法

-(void)passValue:(NSString*)string{

//把收到的string赋值给输入框

self.table.text= string;

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

*******************************************************************************

//

//  TwoViewController.h

#import

#warning协议1:声明协议(定义一个带参数的方法)

@protocolPassDelegate

//必须要实现的@required(默认)

//@optional可选的

-(void)passValue:(NSString*)string;//需要传什么数据就设置什么属性

@end

@interfaceTwoViewController :UIViewController

#warning属性1:在第二页声明一个属性用来保存数据

@property(nonatomic,copy)NSString*string;

#warning协议2:定义代理人属性

@property(nonatomic,assign)iddelegate;

@end

*********************************************

TwoViewController.m

#import"TwoViewController.h"

@interfaceTwoViewController()

@property(nonatomic,retain)UITextField*table2;

@end

@implementationTwoViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor= [UIColorwhiteColor];

self.title=@"第二页";

self.table2= [[UITextFieldalloc]initWithFrame:CGRectMake(60,30,260,40)];

self.table2.backgroundColor= [UIColorwhiteColor];

[self.viewaddSubview:self.table2];

[self.table2release];

self.table2.layer.borderColor= [UIColorlightGrayColor].CGColor;

self.table2.layer.borderWidth=1;

self.table2.layer.cornerRadius=5;

UIButton*btn = [UIButtonbuttonWithType:UIButtonTypeSystem];

btn.frame=CGRectMake(60,120,260,40);

btn.backgroundColor= [UIColorredColor];

[self.viewaddSubview:btn];

[btnaddTarget:selfaction:@selector(goBack)forControlEvents:UIControlEventTouchUpInside];

#warning属性3:通过属性给当前页面赋值

self.table2.text=self.string;

}

-(void)goBack{

#warning协议3:返回上一页之前代理人调用协议方法

[self.delegatepassValue:self.table2.text];//self.delegate相当于rootVC页面执行pass方法

[self.navigationControllerpopToRootViewControllerAnimated:YES];

}

你可能感兴趣的:(导航栏/界面传值)