【发布时间】:2015-08-14 02:31:30
【问题描述】:
当我将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返回结果时,并没有触发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 会触发它
【问题讨论】:
-
我认为你也应该传递 SimpleViewController 因为委托方法也需要视图控制器。它在 SimpleViewController 上工作,因为你传递了“他”,但它不工作,因为你传递了“HelperFunctions”对象。 (哦等等,代理!只需使用 paypalDelegate 而不是 self。)
-
在你写
[paypalDelegate performSelector:@selector(payPalPaymentDidCancel:) withObject:self];的那一行,self是指什么?毕竟,它在类方法中。
标签: ios objective-c paypal