美文网首页
iOS纯代码页面跳转传值(1、顺传,2、逆传—运用代理传值)

iOS纯代码页面跳转传值(1、顺传,2、逆传—运用代理传值)

作者: 广益散人 | 来源:发表于2016-04-23 12:14 被阅读516次

iOS界面传值有很多种方法,我只记录我现在用过的方法,方便以后查阅巩固。

1、顺传

假如项目中需要从界面1条转到界面2,且需要将界面1中的某个值传入到界面2的某个值,这种叫顺传,则只需要在跳转的地方给界面2中的属性赋值就好了。代码如下:

(1)定义界面1

#import "ViewController.h"

#import "SecondViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UILabel * label1;

@property(nonatomic,strong)UIButton *nextBtn;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

[self createUI];

}

-(void)createUI{

_label1 = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];

_label1.backgroundColor = [UIColor lightGrayColor];

_label1.text = @"label1";

[self.view addSubview:_label1];

_nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 350,100, 100)];

_nextBtn.backgroundColor = [UIColor redColor];

[_nextBtn setTitle:@"顺传值" forState:UIControlStateNormal];

[_nextBtn addTarget:self action:@selector(clickedBtn) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_nextBtn];

}

(2)定义界面2

在界面2的.h文件中添加成员属性

@interface SecondViewController : UIViewController

@property(nonatomic,strong)UITextField *textfile;

@property(nonatomic,strong)UILabel *label2;

@property(nonatomic,strong)NSString *str;

在界面2的.m文件中初始化界面

#import "SecondViewController.h"

#import "ViewController.h"

@interface SecondViewController ()

@property(nonatomic,strong)UIButton *preBtn;

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

[self createUI];

}

-(void)createUI{

_textfile = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 200, 40)];

_textfile.backgroundColor = [UIColor lightGrayColor];

_textfile.delegate = self;

_textfile.placeholder = @"请输入你想输入的字符";

[self.view addSubview:_textfile];

_label2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 250, 200, 40)];

_label2.backgroundColor = [UIColor lightGrayColor];

_label2.text = str;

[self.view addSubview:_label2];

_preBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];

_preBtn.backgroundColor = [UIColor redColor];

[_preBtn setTitle:@"逆传值" forState:UIControlStateNormal];

[_preBtn addTarget:self action:@selector(clickedBtn) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_preBtn];

}

界面已经出来 点击顺传值就将label的值传入到界面2点label2中

实现方法代码如下:

-(void)clickedBtn{

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

secondView.passDelgate = self;

NSString *text = _label1.text;

secondView.str = text;

[self presentViewController:secondView animated:YES completion:^{

}];

}

2逆传值(通过代理)

逆传值一共分为五步:1、定义协议、定义代理方法、2、设置协议成员属性 3、指定需要传入到上一个界面的值 4继承协议(包括将自己设置为代理)  5实现协议方法

基于上面的界面,在界面1跳转到界面2后,点击逆传值需要将界面跳回到界面1,并且需要将界面2的textfile的值传给界面1点label1。此时我们就可以运用代理传值。

(1)在界面2的.h文件中定义协议,设置协议成员属性。

#import//代理协议

@protocol passValueDelegate

//代理传值方法

-(void)passVlaue:(NSString *)values;

@end

@interface SecondViewController : UIViewController

@property(nonatomic,strong)UITextField *textfile;

@property(nonatomic,strong)UILabel *label2;

//定义代理属性

@property(nonatomic,strong)id<passValueDelegate>passDelegate;

@end

(2)指定需要传入的值

此时在界面2的页面跳转的点击事件指定

//点击事件实现方法,这里需要指定将本界面中的哪一个值传入到上一个界面中

-(void)clickedBtn{

//指定传哪个值

[self.passDelgate passVlaue:_textfile.text];

//界面跳转

[self dismissViewControllerAnimated:YES completion:^{

}];

}

(3)界面1继承协议

t#import "SecondViewController.h"

@interface ViewController : UIViewController<passValueDelegate>

@end

(4)在初始化界面2 的时候将界面1设置为代理

-(void)clickedBtn{

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

secondView.passDelgate = self;

NSString *text = _label1.text;

secondView.label2.text = text;

[self presentViewController:secondView animated:YES completion:^{

}];

}

(5)实现代理方法

界面1的.m文件中实现代理方法

-(void)passVlaue:(NSString *)values{

self.label1.text = values;

}

相关文章

网友评论

      本文标题:iOS纯代码页面跳转传值(1、顺传,2、逆传—运用代理传值)

      本文链接:https://www.haomeiwen.com/subject/wrgxrttx.html