如何解决为什么子类化主 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 举报,一经查实,本站将立刻删除。