Passing an objective-c protocol delegate as a parameter
When I pass the objective-c protocol as a parameter, when this delegate is then used to run one of its methods, the method fails.
I am using a delegate from the Paypal iOS SDK, which is defined like this:
@protocol PayPalPaymentDelegate <NSObject>
- (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController;
@end
I have a UIViewController that implements the protocol:
//simpleviewcontroller.h
@interface SimpleViewController : UIViewController<PayPalPaymentDelegate>
and method signatures like:
// HelperFunctions.m
+ (void) doSomething: (id<PayPalPaymentDelegate>)paypalDelegate
{
// the selector 'payPalPaymentDidCancel' DOES fire
[paypalDelegate performSelector:@selector(payPalPaymentDidCancel:) withObject:self]
}
// called from simpleviewcontroller.m that implements PayPalPaymentDelegate
[HelperFunctions doSomething: self]
The problem is that when paypal returns the result, it doesn't call the delegate:
// code inside of the 'success' block of an AFNetworking call
PayPalPaymentViewController *paymentViewController;
paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment
configuration:paypalConfiguration
delegate:paypalDelegate];
PaypalDelegate passed to 'delegate' never starts paypal here. Whereas it starts if executed with simpleviewcontroller.m and not with the doSomething method
source to share
Once you set up paymentViewController
, do you hold back paypalDelegate
somewhere? PayPalPaymentViewController
only supports a weak reference to a delegate, so unless it is released (and therefore none of them fire) ARC at the end of the scope that creates PayPalPaymentViewController
.
source to share