场景:需要在视图控制器上执行CustomWebview委托。如何在iPhone中使用协议代替代理
请帮我这个代码。而不是使用回调,我需要使用“协议”。可以这样做,否则我们只能在这种情况下使用回调。
在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];
}
我知道上面的代码可具有协议,在这种情况下我不不需要使用CALLBACK。只是不知道如何实施Protocol。您可以请帮助更改代码通过使用协议而不是CALLBACK。 – iPhoneDev 2010-07-16 08:43:47
你的意思是你想让你自己的类有一个委托协议(独立于'UIWebViewDelegate')?如果是这样,请定义协议(如上所示),将您的委托属性的类型从'id'更改为'id',并使用'[self.delegate performSelector:@selector(theDelegateMethod :) withObject:self]'或'objc_msgSend(self.delegate,@selector(theDelegateMethod :),self)'将消息发送给委托。 (如果协议将该方法定义为可选,请务必检查是否可以先用'[self.delegate respondsToSelector:@selector(theDelegateMethod :)]'调用它!) –
2010-07-16 16:13:55
In MyWebView.h @protocol webProtocol - (void)loadWeb; @end @property(nonatomic,assign)id delegate ;.现在我该如何在“ViewControler.m”中实现这个功能 –
iPhoneDev
2010-07-19 10:51:23