为什么子类化主 ViewController 的视图会导致委托永远不会触发?

问题描述

我制作了一个 customView,它实现了自己的委托,并将其设置在主 viewController 中。它运行良好,这是标准做法,无需显示任何代码

为了更好地组织代码,我创建了一个名为 customManager 的管理器,其中包含 customView 和主 viewController 的 view。 >

我没有在 viewController 中包含所有需要的代码(管理 customView 的设置方式并监听其委托),而是将所有代码移到 customManager 中。这就是为什么我创建 customManager 只是为了清除 viewController 中的代码

所有都正确呈现,但委托永远不会触发。

CustomManager.h

#import "CustomView.h"

@interface CustomManager : NSObject <CustomViewDelegate>

@property (weak) UIView *view; // viewController's view

@property (strong) CustomView *customView;

- (void)load;

@end

CustomManager.m

#import "CustomManager.h"

@implementation CustomManager

// Initialization Method

- (void)load {

    _customView = [[CustomView alloc] init];

    _customView.delegate = self; // !!!

    _customView.frame = CGRectMake(0.0,20.0,_view.frame.size.width,_view.frame.size.height - 20.0);

    [_view addSubview:_customView];
}

#pragma mark - CustomViewDelegate

// Delegate Methods

@end

ViewController.h

#import "CustomManager.h"

@interface ViewController : UIViewController

@property (strong) CustomManager *customManager;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

    _customManager = [[CustomManager alloc] init];

    _customManager.view = self.view;

    [_customManager load];

    // 1. The load method above removes the code needed here for initializing the customView and setting it up correctly.
    // 2. The delegate methods are Now in the customManager and leave the space below clear.
}

@end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)