@Protocol is not working

Asked

Viewed 36 times

0

I am implementing a protocol but the same is not able to make the call from delegate:

if ([self.delegate respondsToSelector:@selector(addCard)]) {
    [self.delegate addCard];
}

Viewcontroller. m

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@protocol ViewControllerDelegate <NSObject>

-(void)addCard;
@end


@interface ViewController : UIViewController{
}

@property (nonatomic, weak) id <ViewControllerDelegate> delegate;


- (IBAction)setCard:(id)sender;

@end




#import "ViewController.h"
#import "PSWallet.h"


@interface ViewController ()

@end

@implementation ViewController
@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



- (IBAction)setCard:(id)sender {

    if ([self.delegate respondsToSelector:@selector(addCard)]) {
        [self.delegate addCard];
    }
}
@end

Pswallet. m

#import <Foundation/Foundation.h>
#import "ViewController.h"


@interface PSWallet : NSObject<ViewControllerDelegate>

@end


#import "PSWallet.h"


@interface PSWallet ()

@end

@implementation PSWallet

-(void)addCard{


    NSLog(@"not work");
}

@end

1 answer

0

Your delegate is a id<ViewControllerDelegate> and the definition of the protocol ViewControllerDelegate the method addCard is not optional. Thus checking [self.delegate respondsToSelector:@selector(addCard)] is theoretically unnecessary.

Your code should work. You are setting the delegate in his ViewController? There is at least one reference strong for him?

Your code compiles and works well here delegate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.