iOS开发之设计模式(一)

本文介绍了iOS开发中的委托设计模式,并通过一个具体的示例——点击HXRedView上的按钮触发控制器页面中的打印操作——详细展示了如何实现该模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从本篇文章开始,我们将陆续讲解iOS开发中使用到的几种主要的设计模式。本篇文章就简单介绍委托设计模式。

简单理解一下委托模式:就是对象A想要做的操作交给对象B去做,其中对象B就是委托对象。也叫做对象A的代理

不是任何对象都能成为对象A的代理。一个对象要想成为对象A的代理是有条件的,就是要遵循对象A的协议(类似Java接口),遵循协议后还要实现协议中的方法。

对委托模式的理解可为下图:

下面我们来通过一个例子:点击HXRedView上的按钮greenButton,在控制器页面中打印内容。

HXRedView.h

#import <UIKit/UIKit.h>

@<span style="color:#FF0000;">protocol</span> HXRedViewDelegate <NSObject>

@optional
- (void)didClickGreenButton;

@end

@interface HXRedView : UIView
/**
 *  代理(委托对象)
 */
<span style="color:#FF0000;">@property (nonatomic, weak) id<HXRedViewDelegate> delegate;</span>

@end

HXRedView.m

#import "HXRedView.h"

@interface HXRedView ()
@property (nonatomic, weak) UIButton *greenButton;

@end

@implementation HXRedView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor redColor];
        
        // 创建按钮
        UIButton *greenButton = [[UIButton alloc] init];
        [greenButton setTitle:@"绿色按钮" forState:UIControlStateNormal];
        [greenButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [greenButton setBackgroundColor:[UIColor greenColor]];
        
        [greenButton addTarget:self action:@selector(clickGreenButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:greenButton];
        self.greenButton = greenButton;
    }
    return self;
}

/**
 *  点击按钮的监听方法
 */
- (void)clickGreenButton {
    
    // 通知代理
    if (<span style="color:#FF0000;">[self.delegate respondsToSelector:@selector(didClickGreenButton)]</span>) {// 代理对象实现了协议
        <span style="color:#FF0000;">[self.delegate didClickGreenButton];</span>
    }
}

/**
 *  布局子控件尺寸和位置
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.greenButton.frame = CGRectMake(50, 50, 200, 80);
}

@end

ViewController.m

#import "ViewController.h"
#import "HXRedView.h"

@interface ViewController ()<span style="color:#FF0000;"><HXRedViewDelegate></span>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 创建HXRedView对象
    HXRedView *redView = [[HXRedView alloc] initWithFrame:CGRectMake(10, 64, 300, 200)];
    
    // 将redView的代理设置为控制器
   <span style="color:#FF0000;"> redView.delegate = self;</span>
    [self.view addSubview:redView];
    
}

/**
 *  代理(自己)实现协议方法
 */
- (void)didClickGreenButton {
    NSLog(@"代理%@实现了协议", self);
}
@end

运行结果:

点击按钮后:

在ViewController.m文件中,创建了HXRedView对象redView之后,设置了redView的代理为控制器

redView.delegate = self;

但是并不是所有对象都能成为redView的代理,控制器要遵循redView的协议才行

@interface ViewController ()<HXRedViewDelegate>

现在代理就可以实现协议中的方法。

Objective-C的协议,类似于C++的抽象类,JAVA的接口。其具体定义如下:

@protocol HXRedViewDelegate<NSObject>

@optional

- (void)didClickGreenButton;

@end

@protocol为协议关键字,HXRedViewDelegate为协议名,didClickGreenButton为协议里的方法。

委托模式就介绍到这里。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值