views:

64

answers:

1

Scenario: Need to execute CustomWebview delegate on view controller.

Please help me with this code. Instead of using callback, I need to use "Protocol". Can it be done or we can only use callback in this scenario.

On 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");

}


On 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

On 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];
}
+2  A: 

Your question is a bit unclear. A protocol and a delegate are two entirely separate, though related, things--apples and oranges. A protocol defines a list of methods an object may or must respond to:

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

A delegate is an object, usually an instance of a custom class, that is handed to another object for custom processing or feedback--that latter object delegates work to the delegate object.


Are you asking how to convert a delegate category on NSObject to a delegate protocol? (The former used to be Apple's pattern for defining the obligations and abilities of a delegate; the latter is the newer way to do the same thing.) If so, it generally looks something like this:

Delegate Category on NSObject

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

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

Delegate Protocol

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


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

Is that what you're looking for? If not, can you clarify exactly what you're trying to do?

Jonathan Grynspan
I know the above code can have protocol, in that case I don't need to use CALLBACK. just don't know how to implement Protocol. Can you please help to change the code by using Protocol instead of CALLBACK.
iPhoneDev
Do you mean you want your own class to have a delegate protocol (separate from `UIWebViewDelegate`)? If so, define the protocol (as shown above), change your delegate property's type from `id` to `id <PROTOCOL_NAME_HERE>`, and use either `[self.delegate performSelector: @selector(theDelegateMethod:) withObject: self]` or `objc_msgSend(self.delegate, @selector(theDelegateMethod:), self)` to send the message to the delegate. (If the protocol defines the method as optional, be sure to check that you can call it with `[self.delegate respondsToSelector: @selector(theDelegateMethod:)]` first!)
Jonathan Grynspan
In MyWebView.h@protocol webProtocol- (void) loadWeb;@end@property (nonatomic, assign) id<webProtocol> delegate;. Now how do I implement this in "ViewControler.m"
iPhoneDev
You should consider reading Apple's Objective-C Primer. It explains this stuff. :) http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/index.html#//apple_ref/doc/uid/TP40007594
Jonathan Grynspan