如何在iPhone中使用协议而不是委托

时间:2010-07-16 06:02:20

标签: iphone protocols

场景:需要在视图控制器上执行CustomWebview委托。

请帮我这个代码。我不需要使用回调,而是需要使用“协议”。可以这样做,或者我们只能在这种情况下使用回调。

在ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    //MyWebView *webView = [[MyWebView alloc] initWithDelegate:self callback:@selector(finishLoading)];

    MyWebView *webView= [[MyWebView alloc] initWithFrame:CGRectMake(0,0,320, 460)];
    [webView LoadURL:@"http://192.168.5.165/"];
    [webView setDelegate:self];
    [webView setCallback:@selector(finishLoading)];
    [self.view addSubview:webView] ; 
}

- (void) finishLoading
{
    NSLog(@"Finish");
}

在MyWebView.h上

@interface MyWebView : UIView<UIWebViewDelegate> {
    NSString *strURL;
}
@property(nonatomic, retain) NSString *strURL;
@property (nonatomic, assign) id delegate;
@property (nonatomic, assign) SEL callback;

-(void) LoadURL:(NSString*)url;
@end

在MyWebView.m

#import "MyWebView.h"

@implementation MyWebView
@synthesize strURL,delegate,callback;
UIWebView *webView;
-(id) initWithFrame:(CGRect)frame
{
    if(self =[super initWithFrame:frame])
    {
        webView = [[UIWebView alloc] initWithFrame:frame];
        webView.delegate = self;
        [self addSubview:webView];

    }
    return self;
}

-(void) LoadURL:(NSString*)url
{
    NSURL *u = [NSURL URLWithString:url];
    NSURLRequest *req= [NSURLRequest requestWithURL:u];
    [webView loadRequest:req];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [delegate performSelector:callback];
}

3 个答案:

答案 0 :(得分:3)

你的问题有点不清楚。协议和委托是两个完全独立的,虽然相关的东西 - 苹果和橘子。协议定义了对象可能或必须响应的方法列表:

@protocol Document
+ (id)documentWithContentsOfURL: (NSURL *)aURL;
- (void)writeToURL: (NSURL *)aURL error: (NSError **)outError;
@end

委托是一个对象,通常是自定义类的一个实例,它被交给另一个对象进行自定义处理或反馈 - 后一个对象委托工作到委托对象。


您是否在询问如何将NSObject上的委托类别转换为委托协议? (前者曾经是Apple定义代表义务和能力的模式;后者是做同样事情的新方法。)如果是这样,它通常看起来像这样:

NSObject上的委托类别

@interface NSObject (WidgetDelegate)
- (void)doSomethingWithWidget: (Widget *)w;
@end

@interface Widget : NSObject
@property (readwrite, assign) id delegate;
@end

代表协议

@protocol WidgetDelegate <NSObject>
- (void)doSomethingWithWidget: (Widget *)w;
@end


@interface Widget : NSObject
@property (readwrite, assign) id <WidgetDelegate> delegate;
@end

这就是你要找的东西吗?如果没有,你能准确澄清你想要做什么吗?

答案 1 :(得分:0)

另请参阅Apple的Communicating with Objects,其中讨论了委托,协议和选择器。虽然它在Mac OS X下列出,但大多数(如果不是全部)似乎也适用于iOS。

答案 2 :(得分:0)

在ViewController.m上

@interface MyViewController : UIViewController<MyFinishLoadingDelegate> {
//your own definition
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //MyWebView *webView = [[MyWebView alloc] initWithDelegate:self callback:@selector(finishLoading)];

    MyWebView *webView= [[MyWebView alloc] initWithFrame:CGRectMake(0,0,320, 460)];
    [webView LoadURL:@"http://192.168.5.165/"];
    [webView setDelegate:self];
    [self.view addSubview:webView] ; 
}

- (void)finishLoading //this is your implementation for MyFinishLoadingDelegate
{
    NSLog(@"Finish");
}

在MyWebView.h上

//you declare the protocol here to tell anyone who'd like to act like your delegate that they need to implement "finishLoading"
@protocol MyFinishLoadingDelegate <NSObject>
- (void)finishLoading;
@end

@interface MyWebView : UIView<UIWebViewDelegate> {
    NSString *strURL;
}
@property(nonatomic, retain) NSString *strURL;
@property (nonatomic, assign) id<MyFinishLoadingDelegate> delegate;

-(void) LoadURL:(NSString*)url;
@end

在MyWebView.m上

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [delegate finishLoading];
}