将 Objective-C 协议委托作为参数传递

Sha*_*ane 5 paypal objective-c ios

当我将 Objective-C 协议作为参数传递时,当该委托用于触发其方法之一时,该方法不会触发。

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

@protocol PayPalPaymentDelegate <NSObject>
    - (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController;
@end
Run Code Online (Sandbox Code Playgroud)

我有一个实现该协议的 UIViewController:

//simpleviewcontroller.h
@interface SimpleViewController : UIViewController<PayPalPaymentDelegate>
Run Code Online (Sandbox Code Playgroud)

以及如下所示的方法签名:

// 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]
Run Code Online (Sandbox Code Playgroud)

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

// code inside of the 'success' block of an AFNetworking call
PayPalPaymentViewController *paymentViewController;
paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment    
                                                             configuration:paypalConfiguration   
                                                             delegate:paypalDelegate];
Run Code Online (Sandbox Code Playgroud)

此处传递给“delegate”的 paypalDelegate 永远不会被 paypal 触发。而如果从 simpleviewcontroller.m 完成而不是通过方法 doSomething 则触发它

Geo*_*een 1

一旦你创建了paymentViewController你会保留paypalDelegate某个地方吗?PayPalPaymentViewController只保留对委托的弱引用,因此如果没有,它将被 ARC 在创建PayPalPaymentViewController.