Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an objective-c protocol delegate as a parameter

When I pass an objective-c protocol as a parameter, when and that delegate is then used to trigger one of its methods, the method does not fire.

I'm using the delegate from the Paypal iOS SDK defined as follows:

@protocol PayPalPaymentDelegate <NSObject>
    - (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController;
@end

I have a UIViewController that implements the protocol:

//simpleviewcontroller.h
@interface SimpleViewController : UIViewController<PayPalPaymentDelegate>

and a method signature like the following:

// 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 does not trigger the delegate:

// code inside of the 'success' block of an AFNetworking call
PayPalPaymentViewController *paymentViewController;
paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment    
                                                             configuration:paypalConfiguration   
                                                             delegate:paypalDelegate];

The paypalDelegate passed to 'delegate' here is never triggered by paypal. Whereas it IS triggered if done from simpleviewcontroller.m and not via the method doSomething

like image 492
Shane Avatar asked Nov 09 '22 12:11

Shane


1 Answers

Once you have created the paymentViewController are you keeping hold of the paypalDelegate somewhere? PayPalPaymentViewController only keeps a weak reference to the delegate so if not it will be being released (and therefore none of it's methods firing) by ARC at the end of the scope that creates the PayPalPaymentViewController.

like image 103
George Green Avatar answered Nov 14 '22 21:11

George Green