Reputation: 3015
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];
}
Upvotes: 0
Views: 1355
Reputation: 301
On 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");
}
On 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
On MyWebView.m
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[delegate finishLoading];
}
Upvotes: 0
Reputation: 102215
Also see Apple's Communicating with Objects, which discusses delegates, protocols, and selectors. Though its listed under Mac OS X, most (if not all) appears to apply to iOS also.
Upvotes: 0
Reputation: 43472
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?
Upvotes: 3