将objective-c协议委托作为参数

时间:2015-05-31 16:34:49

标签: ios objective-c paypal

当我传递一个objective-c协议作为参数时,当该委托用于触发其中一个方法时,该方法不会触发。

我正在使用Paypal iOS SDK中的委托,如下所示:

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

我有一个实现协议的UIViewController:

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

和方法签名如下:

// 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]

问题是,当paypal返回结果时,它不会触发委托:

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

这里传递给'delegate'的paypalDelegate永远不会被paypal触发。然而,如果从simpleviewcontroller.m完成而不是通过方法doSomething

触发它

1 个答案:

答案 0 :(得分:1)

创建paymentViewController后,您是否在某处保留paypalDelegatePayPalPaymentViewController只保留对委托的弱引用,因此如果不是,它将在创建PayPalPaymentViewController的范围结束时被ARC释放(因此没有任何方法触发)。