当我将 objective-c 协议(protocol)作为参数传递时,然后使用该委托(delegate)来触发其方法之一时,该方法不会触发。
我正在使用来自 Paypal iOS SDK 的委托(delegate)定义如下:
@protocol PayPalPaymentDelegate <NSObject>
- (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController;
@end
我有一个实现协议(protocol)的 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返回结果的时候,没有触发委托(delegate):
// 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 则触发它
最佳答案
一旦您创建了 paymentViewController
,您是否在某处保留了 paypalDelegate
? PayPalPaymentViewController
仅保留对委托(delegate)的弱引用,因此如果没有,它将在创建 PayPalPaymentViewController的范围末尾由 ARC 释放(因此不会触发任何方法)
.
关于ios - 将 objective-c 协议(protocol)委托(delegate)作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30559820/