属性传值

正向传值时,将第一页的textField设置为全局变量,
第二页中要在.h文件中定义一个属性用于接收第一页的textField的内容,
然后把接收到的内容传给用于接收的label的text;

反向传值时,
1.当两个界面之间相互传值时,例如 firstViewController和secondViewController。
2,首先,我们需要在secondViewController.h中定义一个协议, @protocol 名字自己定,然后写一个协议方法,
3,接下来我们需要继续在.h文件中定义一个属性,是id类型的(任意对象类型),并且遵守上边的协议。属性名为delegate。
4,然后到secondViewController.m文件中,在这个界面的点击方法中,需要用我们的协议方法去接收这个页面的值,并且存在self.delegate里边,(注:delegate是可以存放任意对象类型的属性)
5,然后到 firstViewController.m文件中,先导入头文件,然后遵守第二个页面签订的协议方法的代理,并且在代码中建立代理关系,
最后我们需要在协议方法中实现本页面的接收,

  • 第一页:
#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<SecondViewControllerDelegate>  //遵守协议

@property(nonatomic,retain)UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    self.textField.placeholder = @"请输入内容:";
    [self.view addSubview:self.textField];
    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 200, 100, 100);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"按钮" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

-(void)buttonAction : (UIButton *) button {
    
    SecondViewController * secondViewController = [[SecondViewController alloc] init];
    //self指当前页面对象,建立代理关系
    secondViewController.delegate = self;
    //用第二个界面的text去接收textField的值
    //把本页面的textField的值赋给secondViewController的text;
    secondViewController.text = self.textField.text;
    [self presentViewController:secondViewController animated:YES completion:nil];
}

//实现协议方法
-(void)getTextFieldValue:(NSString *)text {
    //等号右边的text就是方法中的text;
    self.textField.text = text;
    
}

@end
  • 第二页的.h文件
#import <UIKit/UIKit.h>
//声明一个协议
@protocol SecondViewControllerDelegate <NSObject>
//协议方法
- (void)getTextFieldValue:(NSString *)text;

@end

@interface SecondViewController : UIViewController
//用于给lable显示用的字符串,接收传过来的值
@property(nonatomic,retain)NSString * text;
//delegate是一个任意对象类型的属性,用于存放第一个界面对象
@property(nonatomic,assign)id <SecondViewControllerDelegate>delegate;

@end
  • 第二页的.m文件:
#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic,retain)UITextField * textField;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor cyanColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    lable.backgroundColor = [UIColor redColor];
    //把接收到的值给lable
    lable.text = self.text;
    [self.view addSubview:lable];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
    self.textField.placeholder = @"请输入内容:";
    [self.view addSubview:self.textField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 300, 100, 50);
    button.backgroundColor = [UIColor grayColor];
    [button setTitle:@"back" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}
-(void)buttonAction : (UIButton *)button {
    //self.delegate是指前一个对象
    [self.delegate getTextFieldValue:self.textField.text];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容