Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

ios - Call Back Of Custom Delegate with share Object is not working

My Custom Delegate (With Share Object) call back is not working? I pasted the code below ...

ViewController.h

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

ViewController.m

#import "ViewController.h"
@interface ViewController () @end
@implementation ViewController

- (IBAction)fetchLocation:(id)sender{
    UserLocation *obj = [UserLocation new];
    [[UserLocation LocationObject] FetchLocation];
}

@end

UserLocation.h (Custom Delegate File)

#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

What mistake did I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You haven't made your viewController to be delegate of UserLocation among the many other things wrong with your code. Anyways, addressing the issue at hand, Adjust your code in ViewController.h as per following (I have added comments for each step):

@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.
}

Also you don't need implementation of -(void)userLocationCallBack in UserLocation.m. When you call delegate method, it will look for implementation of that method in the VC which is the delegate of UserLocation. Learn about delegates in a very interesting way here. Also see apple Documentation on it to clear your concepts. Also Learn about Design patterns at this highly recommended link.

Adding up, as recommended sagely by trojanfoe and I quote:

"If this is the only use of the delegate then it's unnecessary in this case; if all that's being done is create instance > call method on instance > get something back, then returning an object is enough. If the delegate can be called at any time then I would expect it to be an instance variable, not a local variable."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...