iOS - 属性传值和协议传值

一 属性传值(正传)

把第一个页面的内容传到第二个页面
第一个页面FirstViewController.h

@property UITextField *accounttextField;

FirstController.m

- (void) press {
	SecondViewController *SecondViewController = [SecondViewController alloc]init];
	registerViewController.str = _accounttextField.text;
	[self presentViewController:registerViewController animated:YES completion:nil];
}

SecondViewController.h

@property NSString *str;  
@property UITextField *accoutTextField;

SecondViewController.m

_accoutTextField.text = _str;

二 协议传值(反传)

从第二个页面传到第一个页面,分六步

SecondViewController.h
//1. 声明代理
@protocol RegisterDelegate <NSObject>

- (void) input:(NSString *)text;

@end

//2.生成一个成员变量,便于访问设置
@property(nonatomic,assign)id <RegisterDelegate> delegate;

SecondViewController.m
在按压按钮的方法中
- (void)touchBtn3:(UIButton*)button
{
    
    
    if([_textField01.text isEqualToString: _textField02.text])
    {
        
        //3. 让代理去执行自己的方法,取得要传的值
        [self.delegate input: _textField01.text];
        [self.delegate input: _textField.text];
        
        [self dismissViewControllerAnimated:YES completion:nil];

FirstViewController.h
//4. 第四步
@interface LandViewController : UIViewController <RegisterDelegate>

FirstViewController.m
//点击第一个页面按钮
- (void)touchBtn2:(UIButton*)button
{
    RegisterViewController *vcRegister = [[RegisterViewController alloc] init];
    
    //5. 设定代理
    vcRegister.delegate = self;

    [vcRegister returnText:^(NSString *showText){
         self->_textField.text = showText;
         self->userStr = showText;
    }];
    [vcRegister returnText2:^(NSString *showText){
         self->_textField01.text = showText;
         self->passStr = showText;
    }];
    
    vcRegister.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vcRegister animated:YES completion:nil];
    //[self.navigationController pushViewController:vcRegister animated:YES];
}

//6. 实现代理方法
- (void) input:(NSString *)text
{
    _textField.text = text;
    _textField01.text = text;
}

        
    }
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页