iOS协议和代理人的例子 [英] iOS- protocols and delegates on the example

查看:102
本文介绍了iOS协议和代理人的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在搜索,但没有任何方法为我工作。以下代码基于许多教程和Apple文档,但我无法让它工作。有人可以帮忙吗?



代码正在崩溃:obj.delegatee = self; (在类Bh中),还有方法respondToSelector和performSelector:withObject不被识别。



我想设置委托对象,当我们点击特定图片。



类啊:

  @interface AViewController :UIViewController< UIScrollViewDelegate> {
id delegatee;
}
@property(nonatomic,assign)id< AViewControllerDelegate>代表团
@end

@protocol AViewControllerDelegate
@optional
- (void)tappedImage:(int)
@end

class Am:

  @dynamic delegatee; 
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
UIImageView * imageView =(UIImageView *)[gestureRecognizer视图];
int a = imageView.tag;

if([self.delegatee respondToSelector:@selector(tappedImage :)])
[self.delegatee performSelector:@selector(tappedImage :) withObject:[NSNumber numberWithInt:a]];

}



class Bh:

  #importAViewController.h
@interface BViewController:UIViewController< AViewControllerDelegate> {...}

class Bm:

   - (void)viewDidLoad 
{
[super viewDidLoad];
// ...一些代码

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;
}

非常感谢任何帮助,我希望它能帮助我了解协议

解决方案

两件事。


  1. (你的崩溃)你宣称你的委托人/ / c $ c>为@dynamic而不是@synthesized,这意味着你负责创建 - (void )setDelegatee:(id< AViewControllerDelegate>)(和getter)方法。修复:只需将它从 @dynamic 更改为 @synthesize delegatee;


  2. (您的警告)每当您想要调用协议中未明确定义的方法时,您的协议将需要符合另一个协议:)。修复:将 NSObject 协议添加到您的decleration。


@protocol AViewControllerDelegate< NSObject>


Ok, i was searching but there wasn't any method that was working for me. Following code bases on many tutorials and Apple documentation, but i can't get it to work. Can anybody help please?

Code is crashing at: obj.delegatee = self; (in class B.h), also methods respondsToSelector and performSelector:withObject aren't recogized.

I want to set delegate object, on which there will be a method called when we tap on particular picture.

class A.h:

@interface AViewController : UIViewController <UIScrollViewDelegate>{
    id delegatee;
}
@property (nonatomic, assign) id <AViewControllerDelegate> delegatee;
@end

@protocol AViewControllerDelegate
@optional
- (void) tappedImage:(int)tag;
@end

class 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]];

}

class B.h:

#import "AViewController.h"
@interface BViewController : UIViewController <AViewControllerDelegate> {...}

class 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;
}

Many thanks for any help, i hope it will help me to understand how protocols work.

解决方案

Two things.

  1. (Your Crash) You declared your delegatee as @dynamic instead of @synthesized which means you are responsible for creating the -(void)setDelegatee:(id<AViewControllerDelegate>) (and the getter) method. Fix: Simply change it from @dynamic to @synthesize delegatee;

  2. (Your warnings) Whenever you want to call methods that you do not explicitly define in your protocol, then your protocol will need to conform to another protocol :). Fix: Add the NSObject protocol to your decleration.

@protocol AViewControllerDelegate<NSObject>

这篇关于iOS协议和代理人的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆