好吧,我正在寻找,但没有任何方法对我有用。以下代码基于许多教程和苹果文档,但我无法让它工作。有人可以帮忙吗?
代码崩溃于: obj.delegatee = self; (在 B.h 类中),respondsToSelector 和 PerformSelector:withObject 方法也无法被识别。
我想设置委托对象,当我们点击特定图片时,该对象上会调用一个方法。
A.h 类:
@interface AViewController : UIViewController <UIScrollViewDelegate>{
id delegatee;
}
@property (nonatomic, assign) id <AViewControllerDelegate> delegatee;
@end
@protocol AViewControllerDelegate
@optional
- (void) tappedImage:(int)tag;
@end
A.m 班:
@dynamic delegatee;
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
UIImageView *imageView = (UIImageView *)[gestureRecognizer view];
int a = imageView.tag;
if ([self.delegatee respondsToSelector:@selector(tappedImage:)])
[self.delegatee performSelector:@selector(tappedImage:) withObject: [NSNumber numberWithInt:a]];
}
B.h 类:
#import "AViewController.h"
@interface BViewController : UIViewController <AViewControllerDelegate> {...}
B.m级:
- (void)viewDidLoad
{
[super viewDidLoad];
//... some code
AViewController *obj = [[[AViewController alloc] init] autorelease];
obj.delegatee = self;
}
- (void) tappedImage:(int)tag{
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: @"%d.png",tag]]];
CViewController *NView = [[CViewController alloc] initWithPicture: imgView.image];
[self presentModalViewController:NView animated:YES];
[NView release]; NView = nil;
}
非常感谢您的帮助,我希望它能帮助我了解协议的工作原理。