iOS底层系列之<52>--MVC的变种

MVC变种代码

1、ViewController
#import "ViewController.h"
#import "JView.h"
#import "Model.h"

@interface ViewController ()<JHViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"begin");
    
    JView *jview = [[JView alloc] init];
    jview.delegate = self;
    jview.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:jview];

    Model *model = [[Model alloc] init];
    model.name = @"张三";
    model.imageName = @"123";

    jview.model = model;
    
    NSLog(@"--");
}

- (void)appViewDidClick:(JView *)jview {
    NSLog(@"点击了");
}
@end
2.Model
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Model : NSObject
@property (copy, nonatomic) NSString *imageName;
@property (copy, nonatomic) NSString *name;
@end

NS_ASSUME_NONNULL_END
#import "Model.h"

@implementation Model

@end

3.JView
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@class Model,JView;

@protocol JHViewDelegate <NSObject>

- (void)appViewDidClick:(JView *)jview;

@end

@interface JView : UIView
@property (strong, nonatomic) Model *model;
@property (weak, nonatomic)id<JHViewDelegate> delegate;
@end

NS_ASSUME_NONNULL_END
#import "JView.h"
#import "Model.h"

@interface JView()
@property (weak, nonatomic) UIImageView *iconV;
@property (weak, nonatomic) UILabel *nameL;

@end

@implementation JView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIImageView *iconV = [[UIImageView alloc] init];
        iconV.frame = CGRectMake(0, 0, 100, 100);
        [self addSubview:iconV];
        _iconV = iconV;
        
        UILabel *nameL = [[UILabel alloc] init];
        nameL.frame = CGRectMake(0, 100, 100, 30);
        nameL.textAlignment = NSTextAlignmentCenter;
        [self addSubview:nameL];
        _nameL = nameL;
    }
    
    return self;
}

- (void)setModel:(Model *)model {
    _model = model;
    
    self.iconV.image = [UIImage imageNamed:model.imageName];
    self.nameL.text = model.name;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self.delegate respondsToSelector:@selector(appViewDidClick:)]) {
        [self.delegate appViewDidClick:self];
    }
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值