IOS笔记-协议的用法

协议概念

在ios中,协议相当于一个方法(函数)的声明,只是该声明不需要在委托方实现(协议发起者),而要在代理方实现(协议遵守方)。
现在假如我要去厨房拿一点水果吃,但是我在写字抽不开身,于是我委托二哈去厨房帮我把水果叼来,在这个过程中,“我”就是委托方,二哈就是代理方,而拿水果就是协议,我们可以告诉二哈去拿苹果或者香蕉,同样,委托方可以通过协议向代理方传递参数,从而让代理方帮我们完成一些事情。

  • 委托方:协议发起方,协议的声明书写在.h中
  • 代理方:在.h中用<>将协议括起
  • 协议:在委托方.h中为声明,在代理方中为函数体

让我们做一个简单的demo

第一个view中有一个橙色的label显示名字
在这里插入图片描述
点击Btn1我们就进入第二个view,左侧为输入框,右侧为Btn2,我们在输入框中输入名字,需要点击Btn2后返回view1并将输入的名字显示到view1的label上
在这里插入图片描述

Demo演示

在这里插入图片描述

代码

view2 .h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

//创建协议
@protocol NameSendDemo <NSObject>
//required表示必须执行的声明
@required
//optional表示可以选择执行的声明
@optional
-(void)sendName:(NSString*)name;

@end

//viewcontroller2接口
@interface ViewController2 : UIViewController

{
    UITextField* txField;
}
//将协议的代理写在这
@property(nonatomic,assign)id<NameSendDemo> delegate;

@end

view2 .m

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    UIButton* Btn2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 200, 50, 30)];
    [Btn2 setTitle:@"Btn2" forState:UIControlStateNormal];
    [Btn2 addTarget:self action:@selector(touch) forControlEvents:UIControlEventTouchDown];
    Btn2.backgroundColor = [UIColor grayColor];
    txField = [[UITextField alloc]initWithFrame:CGRectMake(80, 200, 100, 30)];
    txField.backgroundColor = [UIColor grayColor];
    [self.view addSubview:txField];
    [self.view addSubview:Btn2];
    
   
}
-(void)touch{
    [self dismissViewControllerAnimated:YES completion:nil];
    //在点击Btn2时候,通过代理将输入框的r内容传入View1
    [self.delegate sendName:txField.text];
}
@end

view1 .h

#import <UIKit/UIKit.h>
#import "ViewController2.h"
@interface ViewController : UIViewController<NameSendDemo>
{
    UILabel* nameLabel;
}
@end

view1 .m

#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 200, 100, 30)];
    nameLabel.backgroundColor = [UIColor orangeColor];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.layer.cornerRadius = 10;
    nameLabel.layer.masksToBounds = YES;
    nameLabel.text = @"你的名字";
    UIButton* Btn1 = [[UIButton alloc]initWithFrame:CGRectMake(200, 200, 50, 30)];
    [Btn1 setTitle:@"Btn1" forState:UIControlStateNormal];
    Btn1.backgroundColor = [UIColor grayColor];
    [Btn1 addTarget:self action:@selector(touch) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:nameLabel];
    [self.view addSubview:Btn1];
}
-(void)touch{
    ViewController2* view2 = [[ViewController2 alloc]init];
    [self presentViewController:view2 animated:YES completion:nil];
    
    //跳转的时候这句千万不能忘
    view2.delegate = self;
    
}

//协议的实现
-(void)sendName:(NSString *)name
{
    //将label的内容更改为传入的name
    nameLabel.text = name;
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值