我试图用initWithFrame
但我不能够显示UIView
,这种代码解决了很多问题,因为我看到这里,但不是在我的情况:包括tapgesture的UIview自定义类可以吗?
- (id)initWithFrame:(CGRect)frame myParams:(NSArray*)params;
.m文件
- (id)initWithFrame:(CGRect)frame myParams:(NSArray*)params{ self = [super initWithFrame:frame]; if(self){ UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; //Trying to send an uiview with gesture include view.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] initWithTarget:self action:@selector(test)]; [view addGestureRecognizer:tap]; [self addSubview:view]; } return self; }
这里是我如何添加视图在UIViewController
: 也许在这里我做错了吧?
Custom *view = [Custom alloc] initWithFrame:self.view.bounds myParams:array]; [self.view addSubview:view];
所以,我的问题是,它可以添加UITapGesture
在这个类中产生的UIView
,因为它不点火:
.h文件中
+ (UIView *)viewParams:(NSArray*)params;
.M文件
+ (UIView *)viewWithParams:(NSArray*)params{ UIView *view = [UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; NSLog(@"read params %@", params); //Trying to send an uiview with gesture include view.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc] initWithTarget:self action:@selector(test)]; [view addGestureRecognizer:tap]; return view; } - (void)test{ NSLog('it works...'); }
更新:实例方法正在工作。但作为类方法我不能使它工作。有人知道可能吗?
你可以尝试给self.view.userInteractionEnabled = YES;在视图控制器? – Arun
这并没有工作既不尝试作为类方法。 – usosystem