界面之间传值

首先要建立两个页面
一个根视图控制器,之后建第二个视图
MainViewController和SecondViewController

界面传值共有两种:

第一种是在第一个界面向第二个页面传值

这种传值相对简单:系统原本就有这种方法
1.在SecondViewController.h文件中定义一个属性来接收传的整数,数组.或者是字符串;
例子

@property(nonatomic ,assign)NSInteger number;
@property(nonatomic ,retain)NSArray *arr;
@property(nonatomic ,copy)NSString *str;

2.在第一个页面中的点击方法中实现传值:
-(void)click:(UIButton *)button{
// 通过导航控制器进行跳转
// 先创建下一页的对象
SecondViewController *secondView=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secondView animated:YES];
[secondView release];
// 属性传值的第二步
secondView.number =100;
secondView.str =self.textfield.text;
secondView.arr =@[@”1”,@”2”,@”3”];
}

第二种是从第二个页面向第一个页面传值

这种传值相对第一种传值方法比较复杂,是因为原本没有这种方法,要自己写一种协议方法
这种传值方法共分为六部:

1.在SecondViewController.h文件中定义一个协议方法:用来传值:

例子:

@protocol SecondViewControllerDelegate <NSObject>
-(void)changeValue:(NSString *)value;
@end
2.设置代理人的属性

在SecondViewController.h文件中
例子:

@property(nonatomic,assign)id<SecondViewControllerDelegate>delegate;
3.设置代理人执行的协议方法

在SecondViewController.m文件中
例子:
-(void)click:(UIButton *)button{
// 返回上一页面
[self.navigationController popViewControllerAnimated:YES];
// 设置代理人执行的协议方法
[self.delegate changeValue:self.textField.text];

}

4.签订协议

例子:

@interface MainViewController ()<SecondViewControllerDelegate>
5.设置代理人

secondVC.delegate =self;
这步需要在创建secondVC的协议方法中设置

6.实现协议方法

例子:

-(void)changeValue:(NSString *)value{
    self.label.text=value;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值