Objective C 特定事件的回调
Objective C Callback on particular event
我是 objective C 的新手,正在尝试开发自己的回调函数,回调函数在特定事件上被调用,例如从网络接收数据,就像 NSURLprotocol
那样,一旦接收到它就会 NSLog 消息“事件已发生”或在 UIViewController
或任何 UI 上显示为文本 相关操作。
所以,我完全不知道应该在哪里调用 eventOccuredMethod
来让 receiveController
被调用并执行其中的实现。
我以前使用过 NSURLProtocol
之类的协议,但我不知道如何实现它们来调用此类回调。
欢迎任何视频链接、答案、文章链接。
//Sample.h file
#import <Foundation/Foundation.h>
@class Sample;
@protocol SampleProtocol <NSObject>
-(void)receivedCallback;
@end
@interface Sample : NSObject
@property (nonatomic,weak) id<SampleProtocol> delegate;
-(void)eventOccured;
@end
//Sample.m 文件
#import "Sample.h"
@implementation Sample
-(void)eventOccured{
if([_delegate conformsToProtocol:@protocol(SampleProtocol)])
[_delegate receivedCallback];
}
@end
//ViewController.h 文件
@interface ViewController : UIViewController<SampleProtocol>
@end
//ViewController.m 文件
#import "ViewController.h"
@interface ViewController (){
Sample *s;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
s = [[Sample alloc] init];
s.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)receivedCallback:(Sample *)sample{
NSLog(@"Event Has Occured");
}
@end
我不确定我正在拨打的以下电话...
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
Sample *s = [[Sample alloc] init];
[s eventOccured];
}
您正在调用 eventOccured
现在已设置委托的第二个独立示例实例。
最简单的解决方法:让视图控制器将它发送到它的示例实例。
更好:给视图控制器一个 属性 来保存来自应用程序委托的样本。
?,我很困惑。我不认为你明白你所写的。在没有先阅读教程以了解您在做什么之前,您永远不应该尝试从网上复制这样的代码。这可能非常危险。
Sample.h / .m
是一个 class,这个 class 定义了一个协议说 "In order for me to alert you to the fact an event has occurred, you need to implement method X".
这是"protocol",通过遵守协议,另一个class(比方说ViewController)说它实现了Sample
的方法寻找。
所以 Sample
将 运行 编码,当它想将一些信息传回给另一个 class(在本例中为 ViewController)时,它会调用其中一个协议中定义的方法。
例如(不是完全有效的代码)
Sample.m
- (void)getDataFromURL:(NSStirng *)url
{
[self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)
{
if([_delegate conformsToProtocol:@protocol(SampleProtocol)])
{
[_delegate receivedCallback];
}
}];
}
所以当Sample
运行方法getDataFromURL
它会请求它的数据,当数据returns,Sample
会调用方法receivedCallback
在它的委托上。在这种情况下,它是 viewController
.
的一个实例
编辑
请注意 [_delegate conformsToProtocol:@protocol(SampleProtocol)]
的作用。这询问委托实例是否符合协议。但是这个协议并没有说 recievedCallback
是必需的。所以你无法知道方法在那里。
要么使用:
@protocol SampleProtocol <NSObject>
@required
-(void)receivedCallback;
@end
在协议定义中或
if(self.delegate && [self.delegate respondsToSelector:@selector(receivedCallback)])
检查是否实施
您应该在数据检索方法中调用 EventOccurred
。数据检索完成后调用 EventOccured。
@protocol SampleProtocol <NSObject>
-(void)receivedCallback;
@end
此协议必须在您的数据检索中实施 class。并确保 -(void)receivedCallback;
有一个参数可以将数据发送到您的 ViewController
您正在以正确的方式实现委托模式。但是,如果 Sample
对象不生成自己的事件,而是中继从其他地方发布给它的事件,就像您的示例中的情况一样,您必须确保具有 [=14= 的对象] 作为委托和接收消息的对象其实是一样的。一种方法是使 Sample
成为单身人士:
#import "Sample.h"
@implementation Sample
+ (instancetype)sharedInstance
{
static id sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
-(void)eventOccured{
if([_delegate conformsToProtocol:@protocol(SampleProtocol)])
[_delegate receivedCallback];
}
@end
然后在你的视图控制器中你会做
s = [Sample sharedInstance];
并在您的 appDelegate 中:
[[Sample sharedInstance] eventOccured];
正如 vikingosegundo 指出的那样,确保您使用相同对象的另一种方法是从 appDelegate 设置视图控制器的 Sample 对象。
对于此用例,您还可以考虑使用通知。
我是 objective C 的新手,正在尝试开发自己的回调函数,回调函数在特定事件上被调用,例如从网络接收数据,就像 NSURLprotocol
那样,一旦接收到它就会 NSLog 消息“事件已发生”或在 UIViewController
或任何 UI 上显示为文本 相关操作。
所以,我完全不知道应该在哪里调用 eventOccuredMethod
来让 receiveController
被调用并执行其中的实现。
我以前使用过 NSURLProtocol
之类的协议,但我不知道如何实现它们来调用此类回调。
欢迎任何视频链接、答案、文章链接。
//Sample.h file
#import <Foundation/Foundation.h>
@class Sample;
@protocol SampleProtocol <NSObject>
-(void)receivedCallback;
@end
@interface Sample : NSObject
@property (nonatomic,weak) id<SampleProtocol> delegate;
-(void)eventOccured;
@end
//Sample.m 文件
#import "Sample.h"
@implementation Sample
-(void)eventOccured{
if([_delegate conformsToProtocol:@protocol(SampleProtocol)])
[_delegate receivedCallback];
}
@end
//ViewController.h 文件
@interface ViewController : UIViewController<SampleProtocol>
@end
//ViewController.m 文件
#import "ViewController.h"
@interface ViewController (){
Sample *s;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
s = [[Sample alloc] init];
s.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)receivedCallback:(Sample *)sample{
NSLog(@"Event Has Occured");
}
@end
我不确定我正在拨打的以下电话...
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
Sample *s = [[Sample alloc] init];
[s eventOccured];
}
您正在调用 eventOccured
现在已设置委托的第二个独立示例实例。
最简单的解决方法:让视图控制器将它发送到它的示例实例。
更好:给视图控制器一个 属性 来保存来自应用程序委托的样本。
?,我很困惑。我不认为你明白你所写的。在没有先阅读教程以了解您在做什么之前,您永远不应该尝试从网上复制这样的代码。这可能非常危险。
Sample.h / .m
是一个 class,这个 class 定义了一个协议说 "In order for me to alert you to the fact an event has occurred, you need to implement method X".
这是"protocol",通过遵守协议,另一个class(比方说ViewController)说它实现了Sample
的方法寻找。
所以 Sample
将 运行 编码,当它想将一些信息传回给另一个 class(在本例中为 ViewController)时,它会调用其中一个协议中定义的方法。
例如(不是完全有效的代码)
Sample.m
- (void)getDataFromURL:(NSStirng *)url
{
[self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)
{
if([_delegate conformsToProtocol:@protocol(SampleProtocol)])
{
[_delegate receivedCallback];
}
}];
}
所以当Sample
运行方法getDataFromURL
它会请求它的数据,当数据returns,Sample
会调用方法receivedCallback
在它的委托上。在这种情况下,它是 viewController
.
编辑
请注意 [_delegate conformsToProtocol:@protocol(SampleProtocol)]
的作用。这询问委托实例是否符合协议。但是这个协议并没有说 recievedCallback
是必需的。所以你无法知道方法在那里。
要么使用:
@protocol SampleProtocol <NSObject>
@required
-(void)receivedCallback;
@end
在协议定义中或
if(self.delegate && [self.delegate respondsToSelector:@selector(receivedCallback)])
检查是否实施
您应该在数据检索方法中调用 EventOccurred
。数据检索完成后调用 EventOccured。
@protocol SampleProtocol <NSObject>
-(void)receivedCallback;
@end
此协议必须在您的数据检索中实施 class。并确保 -(void)receivedCallback;
有一个参数可以将数据发送到您的 ViewController
您正在以正确的方式实现委托模式。但是,如果 Sample
对象不生成自己的事件,而是中继从其他地方发布给它的事件,就像您的示例中的情况一样,您必须确保具有 [=14= 的对象] 作为委托和接收消息的对象其实是一样的。一种方法是使 Sample
成为单身人士:
#import "Sample.h"
@implementation Sample
+ (instancetype)sharedInstance
{
static id sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
-(void)eventOccured{
if([_delegate conformsToProtocol:@protocol(SampleProtocol)])
[_delegate receivedCallback];
}
@end
然后在你的视图控制器中你会做
s = [Sample sharedInstance];
并在您的 appDelegate 中:
[[Sample sharedInstance] eventOccured];
正如 vikingosegundo 指出的那样,确保您使用相同对象的另一种方法是从 appDelegate 设置视图控制器的 Sample 对象。
对于此用例,您还可以考虑使用通知。