iOS 属性传值和协议传值

属性传值

属性传值是一种正向传值的方法,当第一个界面向第二个界面跳转时,第一个界面将一些数据传给第二个界面,这就是正向传值。

思路

  1. 创建两个ViewController,在第二个界面中定义一个属性来接收第一个界面传过来的信息
  2. 创建第一个界面中的button点击事件,并在这里进行传值

具体代码

  • ViewController.h中
@interface ViewController : UIViewController

@property(nonatomic, strong)UITableView *tableView;
@property UITextField *nameTextField;

@end
  • ViewController.m中
#import "ViewController.h"
#import "MyViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //创建一个button,跳转到第二个界面
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:nextBtn];
    //添加点击事件
    [nextBtn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.frame = CGRectMake(160, 500, 70, 30);
    nextBtn.backgroundColor = [UIColor blackColor];
    nextBtn.tintColor = [UIColor whiteColor];
    [nextBtn setTitle:@"NEXT" forState:UIControlStateNormal];
    
    //创建一个TextField,用户输入
    _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 330, 330, 50)];
    _nameTextField.layer.borderWidth = 2;
    _nameTextField.layer.cornerRadius = 10;
    _nameTextField.delegate = self;
    _nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_nameTextField];
}

-(void) next{
    MyViewController *myVC = [[MyViewController alloc]init];
    //传值
    myVC.nameStr = _nameTextField.text;
    //第二个界面设置满页显示
    myVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:myVC animated:YES completion:nil];
}
  • MyViewController.h中
@interface MyViewController : UIViewController
//接收第一个界面传的值
@property NSString *nameStr;

@end
  • MyViewController.m中
#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    backBtn.frame = CGRectMake(160, 500, 70, 30);
    backBtn.backgroundColor = [UIColor blackColor];
    backBtn.tintColor = [UIColor whiteColor];
    [backBtn setTitle:@"Back" forState:UIControlStateNormal];
    
    //创建一个label用来显示传过来的nameStr
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(180, 300, 80, 20)];
    [self.view addSubview:nameLabel];
    nameLabel.text = _nameStr;
    
}

-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}

协议传值

协议传值是反向传值的一种,第二个界面需要将将一些数据反向传递给第一个界面,这就是反向传值。

思路

  1. 在第二个界面定义一个协议,在协议里面定义一个方法,这个方法的具体作用就是传值,并在第二界面中声明一个遵守该协议的属性。
  2. 在第一个界面中代理协议
  3. 在一个界面初始化第二个界面时,设置代理对象
  4. 在第一个界面中在实现协议传值方法
  5. 在第二个界面中通过代理传值

具体代码

  • 在MyViewController.h中定义协议
#import "ViewController.h"
NS_ASSUME_NONNULL_BEGIN

@protocol ReturnDelegate <NSObject>

//协议中的方法,实现传值
- (void) backInfo: (NSString *)nameStr;

@end

@interface MyViewController : UIViewController
@property NSString *nameStr;
@property UITextField *backTextField;
//定义遵守协议的属性
@property id <ReturnDelegate> returnDelegate; 

@end
  • 在ViewController.m中代理协议,设置代理对象,实现协议传值的方法
#import "ViewController.h"
#import "MyViewController.h"

@interface ViewController ()<UITextFieldDelegate, ReturnDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:nextBtn];
    [nextBtn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.frame = CGRectMake(160, 500, 70, 30);
    nextBtn.backgroundColor = [UIColor blackColor];
    nextBtn.tintColor = [UIColor whiteColor];
    [nextBtn setTitle:@"NEXT" forState:UIControlStateNormal];
    
    
    _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 330, 330, 50)];
    _nameTextField.layer.borderWidth = 2;
    _nameTextField.layer.cornerRadius = 10;
    _nameTextField.delegate = self;
    _nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_nameTextField];
    
}

//实现方法
- (void) backInfo: (NSString *)nameStr{
    _nameTextField.text = nameStr;
}

-(void) next{
    MyViewController *myVC = [[MyViewController alloc]init];
    myVC.nameStr = _nameTextField.text;
    //代理对象
    myVC.returnDelegate = self;
    myVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:myVC animated:YES completion:nil];
}
  • MyViewController.m中,通过代理实现传值
#import "MyViewController.h"

@interface MyViewController ()<UITextFieldDelegate>

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    backBtn.frame = CGRectMake(160, 500, 70, 30);
    backBtn.backgroundColor = [UIColor blackColor];
    backBtn.tintColor = [UIColor whiteColor];
    [backBtn setTitle:@"Back" forState:UIControlStateNormal];
    
    _backTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 380, 330, 50)];
    _backTextField.layer.borderWidth = 2;
    _backTextField.layer.cornerRadius = 10;
    _backTextField.delegate = self;
    _backTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_backTextField];
    
}

//点击back时返回第一个界面传值
-(void)back{
    if ([_returnDelegate respondsToSelector:@selector(backInfo:)]) {
        [_returnDelegate backInfo:_backTextField.text];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值