是否可以从另一个类调用@selector 方法?例如,我创建了一个方法“bannerTapped:”并从“myViewController.m”类中调用它。
myviewcontroller.m:
anotherClass *ac= [[anotherClass alloc]init];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:ac action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
cell.conversationImageView.tag = indexPath.row;
[cell.conversationImageView addGestureRecognizer:singleTap];
[cell.conversationImageView setUserInteractionEnabled:YES];
另一个类.m:
-(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
//do something here
}
更新:
视图控制器.m:
#import "anotherClass.h"
+(viewcontroller *)myMethod{
// some code...
anotherClass *ac= [[anotherClass alloc]init];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:ac action:@selector(bannerTapped:)];
}
另一个类.h:
-(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer;
另一个类.m:
-(void)Viewdidload{
[viewController myMethod];
}
-(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
//do something here
}