iOS简单的传值

1.协议传值

从twoViewController传到viewController

  • twoViewController.h
    
    #import <UIKit/UIKit.h>
    
    //声明一个协议
    @protocol twoViewControllerDelegate <NSObject>
    
    //自定义方法来反向传值
    - (void)changeWithString:(NSString *)string;
    
    @end
    
    
    @interface twoViewController : UIViewController
    
    @property (nonatomic, strong) UITextField *firstTextField;
    
    @property (nonatomic, strong) UITextField *secondTextField;
    
    @property (nonatomic, strong) UIButton *button;
    
    //定义属性delegate 用weak修饰防止循环引用
    @property (nonatomic, weak) id <twoViewControllerDelegate> delegate;
    
    
    @end
    
    
    
    twoViewController.m
    
    #import "twoViewController.h"
    
    @interface twoViewController ()
    
    @end
    
    @implementation twoViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = [UIColor yellowColor];
        self.firstTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 200, 50)];
        _firstTextField.backgroundColor = [UIColor whiteColor];
        _firstTextField.layer.masksToBounds = YES;
        _firstTextField.layer.cornerRadius = 7;
        [self.view addSubview:_firstTextField];
        
        self.secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 200, 50)];
        _secondTextField.layer.masksToBounds = YES;
        _secondTextField.backgroundColor = [UIColor whiteColor];
        _secondTextField.layer.cornerRadius = 7;
        [self.view addSubview:_secondTextField];
        
        self.button = [[UIButton alloc] initWithFrame:CGRectMake(50, 270, 100, 50)];
        self.button.backgroundColor = [UIColor blueColor];
        [self.button setTitle:@"点击" forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button];
    }
    
    - (void)clickButton:(UIButton *)sender {
        [self.navigationController popViewControllerAnimated:YES];
        //先判断协议方法是否已经响应
        if ([_delegate respondsToSelector:@selector(changeWithString:)]) {
            //代理传值
            [_delegate changeWithString:self.firstTextField.text];
            
        }
       
    }
    ViewController.h
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @property (nonatomic, strong)UITextField *textField;
    
    @property (nonatomic, strong)UIButton *button;
    
    
    @end
    
    
    
    ViewController.m
    
    
    #import "ViewController.h"
    #import "twoViewController.h"
    
    @interface ViewController ()<twoViewControllerDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        self.view.backgroundColor = [UIColor yellowColor];
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 200, 50)];
        self.textField.backgroundColor = [UIColor whiteColor];
        self.textField.layer.masksToBounds = YES;
        self.textField.layer.cornerRadius = 7;
        [self.view addSubview:_textField];
        
        
        self.button = [[UIButton alloc] initWithFrame:CGRectMake(40, 160, 100, 50)];
        [_button setTitle:@"点击" forState:UIControlStateNormal];
        self.button.backgroundColor = [UIColor greenColor];
        [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_button];
    }
    
    - (void)click:(UIButton *)sender {
        twoViewController *aController = [[twoViewController alloc] init];
        //遵守协议
        aController.delegate = self;
        [self.navigationController pushViewController:aController animated:YES];
    }
    
    //实现协议中的方法
    - (void)changeWithString:(NSString *)string {
        self.textField.text = string;
    }
    

    后面代码中不再有控件的实现,只是传值的重点

2.属性传值

从ViewController传到twoViewController

//在twoViewController.h中声明一个属性
@property (nonatomic, copy)NSString *string;

//在twoViewController.m 中你要传值的地方
self.firstTextField.text = self.string;



//在ViewController.m中点击button跳转函数中
- (void)click:(UIButton *)sender {
    twoViewController *aController = [[twoViewController alloc] init];
    //把textField.text 传给跳转页面的一个属性
    aController.string = self.textField.text;
    [self.navigationController pushViewController:aController animated:YES]
}

3.通知传值

从twoViewController传到ViewController

//在twoViewController.m中点击button时
- (void)clickButton:(UIButton *)sender {
    
    NSDictionary *dict = @{@"text":self.firstTextField.text};
    //发送通知 userInfo 为附加属性 为字典类型
    //因为有时候要传递的参数不是一个,在接受者中根据字典里面的键来取出里面的值
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pass" object:nil userInfo:dict];
    [self.navigationController popViewControllerAnimated:YES];
}


//在ViewController.m中
//viewDidLoad中注册监听者,name要和发送通知页面(twoViewController)的name相同,用来区分时哪一个通知,object 的值为nil时,代表监听任何对象发送的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pass:) name:@"pass" object:nil];
    

在接受者中实现通知中的方法
- (void)pass:(NSNotification *)text {
    //如果时传递多个数据,将对应中数据取出来即可
    self.textField.text = text.userInfo[@"text"];
}

4.block传值

从twoViewController传到ViewController

twoViewController.h

#import <UIKit/UIKit.h>

//重新定义一个名字
typedef void (^ReturnTextBlock)(NSString *showText);

@interface twoViewController : UIViewController

@property (nonatomic, strong) UITextField *firstTextField;

@property (nonatomic, strong) UITextField *secondTextField;

@property (nonatomic, strong) UIButton *button;
//定义一个block属性
@property (nonatomic,copy) ReturnTextBlock returnTextBlock;

- (void)returnText:(ReturnTextBlock)block;


@end


twoViewController.m

//把传进来的Block语句块保存到本类的实例变量returnTextBlock中
-(void)returnText:(ReturnTextBlock)block{
    self.returnTextBlock = block;
}

//当视图即将消失时 重写
-(void)viewWillDisappear:(BOOL)animated{
    if (self.returnTextBlock != nil) {
        self.returnTextBlock(self.firstTextField.text);
        
    }
}


ViewController.m
- (void)click:(UIButton *)sender {
    twoViewController *aController = [[twoViewController alloc] init];
    
    //回掉方法将输入框中的数据传给textField
    [aController returnText:^(NSString *showText) {
        self.textField.text = showText;
    }];
    
    [self.navigationController pushViewController:aController animated:YES];
}

猜你喜欢

转载自blog.csdn.net/twier_/article/details/81564154