0
我已經在一個類中做了一些@properties的uiimage視圖類型,然後在我的rootviewcontroller中初始化這個類。使用類方法進行封裝?
questions.h:
@property(nonatomic,strong)UIImageView *img1;
@property(nonatomic,strong)UIImageView *img2;
@property(nonatomic,strong)UIImageView *img3;
@property(nonatomic,strong)UIImageView *img4;
'viewcontroller.m'
-(void)viewdidload{
questions *q1=[[questions alloc]init];
q1.img1 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red.png"]];
[self.view addSubview:q1.img1];
UITapGestureRecognizer *Tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iTap:)];
[q1.img1 addGestureRecognizer:Tap1];
}
-(void)iTap:(UIGestureRecognizer *)gestureRecognizer {
// do something..
}
我知道它不是一個好主意來填充初始化代碼viewDidLoad中.. 有沒有更好的方式來acive這使用類的方法?
,如:
questions.m:
+(questions *)mymethod{
questions *q1=[[questions alloc]init];
q1.img1 =[[uiimageview alloc]initwithimage [uimage [email protected]"red.png"]];
[self.view addSubview:q1.img1];
UITapGestureRecognizer *Tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iTap:)];
[q1.img1 addGestureRecognizer:Tap1];
return q1;
}
,然後調用viewDidLoad中該方法
viewcontroller.m:
-(void)viewdidload{
[questions mymethod];
}
您應該首先修復代碼中最明顯的錯誤。 – CouchDeveloper
是的,我知道...請回答我的問題,總之 – user3266009
'initWithTarget:self'在類方法中不起作用。 – Akhilrajtr