好的,我正在搜索,但是没有任何方法适用于我。以下代码基于许多教程和苹果文档,但我无法使其正常工作。有人可以帮忙吗?
代码在 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 类的头文件 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;
}
非常感谢你的帮助,我希望这能帮助我理解协议是如何工作的。