一般自定义view的点击事件触发都会经过touchevent事件。
事件处理只需要重写这个这些方法就好。
比如重写
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
这个方法
而一般在viewcontroller里使用view的点击事件的时候,都会使用delegate的方式来处理。
今天主要结束下。利用block来处理简单的view的事件处理。
新建一个view继承 UIView ,声明一个block,并创建一个初始化方法。初始化方法传入点击事件的block。
.h文件
@interface moreView: UIView { void (^moreViewClick)();//声明一个block }
在.m文件中实现初始化方法,并在touchevent方法中使用block- (id)initWithFrame:(CGRect)frame :( void (^) () ) moreclick;
.m文件代码
- (id)initWithFrame:(CGRect)frame :( void (^) () ) moreclick{ self = [super initWithFrame:frame];
//给view里的block赋值 moreViewClick = moreclick; if (self) { // Initialization code= 处理自己的逻辑 } return self; }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; NSLog(@"more点击结束");//在此处使用点击block moreViewClick(); }
在viewcontroller里使用
创建的时候直接初始化出block即可。
moreView *more = [[moreView alloc] initWithFrame:CGRectMake(0, 0, AppWidth-10, 30) :^{ NSLog(@"more点击结束"); CategoryListView *cv = [[CategoryListView alloc] init]; [self.navigationController pushViewController:cv animated:YES]; }];