如何在iPhone中使用协议代替委托
场景:需要在视图控制器上执行CustomWebview委托 请帮我输入这个代码。我需要使用“协议”,而不是使用回调。在这种情况下,我们只能使用回调 在ViewController.m上如何在iPhone中使用协议代替委托,iphone,protocols,Iphone,Protocols,场景:需要在视图控制器上执行CustomWebview委托 请帮我输入这个代码。我需要使用“协议”,而不是使用回调。在这种情况下,我们只能使用回调 在ViewController.m上 - (void)viewDidLoad { [super viewDidLoad]; //MyWebView *webView = [[MyWebView alloc] initWithDelegate:self callback:@selector(finishLoading)]; 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");
}
#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];
}
在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
你的问题有点不清楚。协议和委托是两个完全独立但又相关的东西——苹果和桔子。协议定义了对象可能或必须响应的方法列表:
@protocol Document
+ (id)documentWithContentsOfURL: (NSURL *)aURL;
- (void)writeToURL: (NSURL *)aURL error: (NSError **)outError;
@end
委托是一个对象,通常是一个自定义类的实例,它被交给另一个对象进行自定义处理或反馈——后者的对象委托给委托对象
您是否询问如何将NSObject上的委托类别转换为委托协议?(前者过去是苹果定义委托人义务和能力的模式;后者是做同样事情的较新方式。)如果是这样的话,通常看起来是这样的: 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
@协议WidgetDelegate
-(void)doSomethingWithWidget:(Widget*)w;
@结束
@接口小部件:NSObject
@属性(读写、分配)id委托;
@结束
这就是你要找的吗?如果没有,你能确切地说明你想做什么吗?你的问题有点不清楚。协议和委托是两个完全独立但又相关的东西——苹果和桔子。协议定义了对象可能或必须响应的方法列表:
@protocol Document
+ (id)documentWithContentsOfURL: (NSURL *)aURL;
- (void)writeToURL: (NSURL *)aURL error: (NSError **)outError;
@end
委托是一个对象,通常是一个自定义类的实例,它被交给另一个对象进行自定义处理或反馈——后者的对象委托给委托对象
您是否询问如何将NSObject上的委托类别转换为委托协议?(前者过去是苹果定义委托人义务和能力的模式;后者是做同样事情的较新方式。)如果是这样的话,通常看起来是这样的: 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
@协议WidgetDelegate
-(void)doSomethingWithWidget:(Widget*)w;
@结束
@接口小部件:NSObject
@属性(读写、分配)id委托;
@结束
这就是你要找的吗?如果没有,你能确切说明你想做什么吗?另请参阅苹果的,其中讨论了委托、协议和选择器。虽然它被列在Mac OS X下,但大多数(如果不是全部的话)似乎也适用于iOS。另请参见苹果的,其中讨论了代理、协议和选择器。虽然它列在Mac OS X下,但大多数(如果不是全部的话)似乎也适用于iOS。在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");
}
关于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");
}
我知道上面的代码可以有协议,在这种情况下,我不需要使用回调。只是不知道如何实现协议。请使用协议而不是回调来帮助更改代码。您的意思是希望您自己的类具有委托协议(与
UIWebViewDelegate
分开)?如果是这样,请定义协议(如上所示),将委托属性的类型从id
更改为id
,并使用[self.delegate performSelector:@selector(theDelegateMethod:)with object:self]
或objc\u msgSend(self.delegate,@selector(theDelegateMethod:),self)
将消息发送给学员。(如果协议将该方法定义为可选方法,请确保检查是否可以使用MyWebView.h@protocol webProtocol-(void)loadWeb中的[self.delegate respondsToSelector:@selector(theDelegateMethod:)]
首先调用该方法)@end@property(非原子,赋值)id delegate;。现在我如何在“VIEWCORDER?M”中实现这一点,你应该考虑阅读Apple的Objto-C入门。它解释了这件事我知道上面的代码可以有协议,在这种情况下,我不需要使用回调。只是不知道如何实现协议。请使用协议而不是回调来帮助更改代码。您的意思是希望您自己的类具有委托协议(与UIWebViewDelegate
分开)?如果是这样,请定义协议(如上所示),将委托属性的类型从id
更改为id
,并使用[self.delegate performSelector:@selector(theDelegateMethod:)with object:self]
或objc\u msgSend(self.delegate,@selector(theDelegateMethod:),self)
将消息发送给学员。(如果协议将该方法定义为可选方法,请确保检查是否可以使用MyWebView.h@protocol webProtocol-(void)loadWeb中的[self.delegate respondsToSelector:@selector(theDelegateMethod:)]
首先调用该方法)@end@property(非原子,赋值)id delegate;。现在我如何在“VIEWCORDER?M”中实现这一点,你应该考虑阅读Apple的Objto-C入门。它解释了这件事