自定义德拉盖特与共享callback对象不工作

我的自定义委托(与共享对象)callback不起作用? 我粘贴下面的代码…

ViewController.h

#import <UIKit/UIKit.h> #import "UserLocation.h" @interface ViewController : UIViewController @end 

ViewController.m

 #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)fetchLocation:(id)sender{ UserLocation *obj = [UserLocation new]; [[UserLocation LocationObject] FetchLocation]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

UserLocation.h(自定义委托文件)

 #import <Foundation/Foundation.h> @protocol UserLocationDelegate <NSObject> -(void)userLocationCallBack; @end @interface UserLocation : NSObject <UserLocationDelegate> { // Delegate to respond back } + (UserLocation *) LocationObject; + (void)removeSharedHelper; - (void)FetchLocation; @property (nonatomic,strong) id<UserLocationDelegate>UserLocationDelegate; @end 

UserLocation.m

 #import "UserLocation.h" @implementation UserLocation static UserLocation * _sharedActivityObject; + (UserLocation *) LocationObject { if(_sharedActivityObject != nil){ return _sharedActivityObject; } _sharedActivityObject = [[UserLocation alloc] init]; [_sharedActivityObject initializeActivityView]; return _sharedActivityObject; } + (void)removeSharedHelper { if (_sharedActivityObject != nil) { _sharedActivityObject = nil; } } -(void)initializeActivityView{ } - (void)FetchLocation{ [_UserLocationDelegate userLocationCallBack]; } -(void)userLocationCallBack { NSLog(@"Good"); } @end 

我不知道我错了什么…先谢谢你的回复

你还没有让你的viewController成为UserLocation的委托代码之间的许多其他的错误。 无论如何,解决手头的问题,根据以下(我已经添加了每个步骤的意见)在ViewController.h中调整您的代码:

 @interface ViewController () <UserLocationDelegate> //Make your VC adopt delegate of UserLocation @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)fetchLocation:(id)sender{ UserLocation *obj = [UserLocation new]; obj.delegate = self; //Set your VC to be delegate of UserLocation [obj FetchLocation]; //Call the method on obj instance } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } //Add delegate method to your VC -(void)userLocationCallBack { //Your code here will run when delegate is fired. } 

你也不需要在UserLocation.m中实现-(void)userLocationCallBack userLocationCallBack。 当你调用委托方法的时候,它会在作为UserLocation委托的VC中寻找那个方法的实现。 在这里以非常有趣的方式了解代表。 另请参阅Apple文档以清除您的概念。 在此高度推荐的链接上了解devise模式。

加上,作为由trojanfoe贤人build议,我引述:

“如果这是委托的唯一用途,那么在这种情况下就没有必要;如果所有这些都是创build实例>对实例的调用方法>返回一些东西,那么返回一个对象就足够了,如果可以调用委托那么我希望它是一个实例variables,而不是一个局部variables。“

你永远不会分配任何东西给UserLocationDelegate 。 首先注意:使用骆驼大小写variables名称( userLocationDelegate )。

假设你想给委托分配一些东西,你必须创build一些采用实际协议UserLocationDelegate 。 我将假设你想分配一个ViewController的实例作为委托。 所以你必须

  • 使ViewController采用UserLocationDelegate
  • 编写一个自定义的userLocationCallBack方法
  • ViewController分配给UserLocationDelegate

所有应该看起来如下所示:

更改ViewController头文件

 #import <UIKit/UIKit.h> #import "UserLocation.h" @interface ViewController : UIViewController <UserLocationDelegate> @end 

更改UserLocation头文件:

 #import <Foundation/Foundation.h> @protocol UserLocationDelegate <NSObject> -(void)userLocationCallBack; @end @interface UserLocation : NSObject + (UserLocation *) LocationObject; + (void)removeSharedHelper; - (void)fetchLocation; @property (nonatomic,strong) id<UserLocationDelegate> userLocationDelegate; @end 

通过添加以下内容来更改ViewController实现文件:

 - (void)userLocationCallBack { NSLog(@"delegate called"); } 

你还必须在某处指定委托,正确的位置可能是ViewController的构造函数,但是我将使用实际实例化UserLocation

 - (IBAction)fetchLocation:(id)sender{ UserLocation *obj = [UserLocation new]; obj.userLocationDelegate = self; [obj fetchLocation]; } 

最后但并非最不重要的使用重命名的属性:

 - (void)fetchLocation{ [self.userLocationDelegate userLocationCallBack]; } 

最重要的注意事项:阅读单身人士,如何( )使用它们,以及如何正确实施。 你搞砸了很糟糕。 另外阅读协议以及它们应该如何使用。 你目前的代码(即使有上面​​的更正)可能不会如预期的那样工作,因为你尝试合并的几乎每个模式都搞砸了。

改变这个你不是设置委托给自己…

 @interface ViewController ()<UserLocationDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)fetchLocation:(id)sender{ UserLocation *obj = [UserLocation new]; obj.delegate = self; [[UserLocation LocationObject] FetchLocation]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)userLocationCallBack { NSLog(@"Good in viewController"); } @end 

在UserLoaction.h中

 #import "UserLocation.h" @implementation UserLocation static UserLocation * _sharedActivityObject; + (UserLocation *) LocationObject { if(_sharedActivityObject != nil){ return _sharedActivityObject; } _sharedActivityObject = [[UserLocation alloc] init]; [_sharedActivityObject initializeActivityView]; return _sharedActivityObject; } + (void)removeSharedHelper { if (_sharedActivityObject != nil) { _sharedActivityObject = nil; } } -(void)initializeActivityView{ } - (void)FetchLocation{ [self userLocationCallBack]; [_delegate userLocationCallBack]; // this will call your userLoacationCallBack implementation in viewcontroller } -(void)userLocationCallBack { NSLog(@"Good"); } @end 

顺便说一句,你为什么要让你的callback方法在同一个类中实现。 你通过在同一个class级中实现它,完全改变了它的意义。 你应该在viewcontroller.m文件中实现 – (void)userLocationCallBack主体