OC设计模式-委托模式

模式分为3大类

1.创建型模式

工厂模式

工厂方法模式

抽象工厂模式

创建者模式

原型模式

单例模式

2.结构型模式

外观模式

适配器模式

代理模式

装饰模式

桥模式

组合模式

享元模式

3.行为模式

模板方法模式

管缠着模式

状态模式

策略模式

职责链模式

命令模式

访问者模式

调停者模式

备忘录模式

迭代器模式

解析器模式



委托模式是结构体模式的一种,主要功能是像block一样有一个回调传值的功能

下面是代码 建立一个视图控制器,2个VIEW

 第一个VIEW有一个BUTTON 还有一个LABEL  button用来推送下一个视图

第二个VIEW有有一个BUTTON 还有一个TEXTFILED button用来返回上一个视图 

效果:通过点击第二个view 在text输入值 返回然后在laberl上看到txt的值


#import <UIKit/UIKit.h>
#import "KViewController.h"
@interface ViewController : UIViewController<Protocol>

@end


#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)nextView:(id)sender {
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle   mainBundle]];
    KViewController *KV=[story instantiateViewControllerWithIdentifier:@"KViewController"];
    KV.argeement=self;
    [self.navigationController pushViewController:KV animated:YES];
   
}
-(void)callback:(NSString *)string{

    self.myLabel.text=string;

}
@end

第二个VIEW

#import <UIKit/UIKit.h>
@protocol Protocol <NSObject>
-(void)callback:(NSString*)string;
@end
@interface KViewController : UIViewController
@property id<Protocol> argeement;
@end


#import "KViewController.h"

@interface KViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myText;

@end

@implementation KViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)goback:(id)sender {
    
    [self.navigationController popViewControllerAnimated:nil];
    
    if (self.argeement != nil && [self.argeement conformsToProtocol:@protocol(Protocol)]) {
        [self.argeement callback:self.myText.text];
    }
    
}


总结:委托模式主要是实现了一个回调传值的作用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值