给UIButton绑定点击事件无响应
月月菜鸟 2017-05-31 03:51:46 我现在正在自学IOS,在尝试理解IOS的MVC模式, 利用 delegate 解耦合.
我在 LoginView 里实现页面的布局, 在 LoginDelegate 里实现 逻辑处理, 但是我把 方法 写在 LoginView 或者 LoginDelegate 里都没有效果,只有把方法写在 系统自动生成的ViewController 里才有效果. 这是怎么回事呢?
项目 自动 生成的文件:
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m,
我 手动 创建的2个文件:
LoginDelegate.h
LoginDelegate.m
LoginView.h
LoginView.m
绑定事件用的方法 addTarget:action:forControlEvents:
下面放代码:
// ViewController 实现文件
- (void)viewDidLoad {
[super viewDidLoad];
__unused LoginView *loginView = [[LoginView alloc] initWithViewController:self];
}
// LoginView 实现文件
- (instancetype)initWithViewController:(UIViewController *)viewController
{
self = [super init];
if (self) {
self.viewController = viewController;
self.delegate = [[LoginDelegate alloc] init];
// 目标-动作对,给button绑定点击方法
[self.button addTarget:self.delegate
action:@selector(test:)
forControlEvents:UIControlEventTouchUpInside];
// 将 button 覆盖到 view 上
[self.viewController.view addSubview:self.button];
return self;
} else {
return nil;
}
}
// LoginDelegate 实现文件
- (void)test:(id)sender
{
NSLog(@"LoginDelegate test!");
}
我把 test: 搬到 ViewController 里,点击 button 就有效, 搬到 LoginDelegate 里就无效.我想解耦合,所以到底是哪里做错了?求 dalao 指导一二!!!!/(ㄒoㄒ)/~~