当self等于NSObject时,委托就会丢失

时间:2016-04-08 19:32:42

标签: ios objective-c delegates facebook-sdk-4.0 nsobject

我正在使用[FBSDKShareAPI shareWithContent:content delegate:self];在Facebook中共享photo,而self等于名为SocialUtils的NSObject

我将直接进入图书馆并放置日志。

NSLog(@"2");
NSLog(@"_sharePhotoContent _delegate intern is!!! %@",_delegate);
FBSDKGraphRequestHandler completionHandler = ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { //Passing this line the delegate is lost.
NSLog(@"3");
NSLog(@"_sharePhotoContent _delegate intern is!!! %@",_delegate);

此印刷品是:

2
_sharePhotoContent _delegate intern is!!! <SocialUtils: 0x7f87f5707580>

3
_sharePhotoContent _delegate intern is!!! (null)

我尝试过同样的事情,但这次自我等于UIViewController并且工作完美,代表仍然存在。

我不明白为什么会发生这种情况,我需要拥有单独的控制器逻辑,否则就意味着在许多UIViewControllers中复制代码。

有人可以启发我,因为这会发生吗?

1 个答案:

答案 0 :(得分:1)

FBSDKShareAPI中的delegate属性被定义为弱。见这里:

https://github.com/facebook/facebook-ios-sdk/blob/master/FBSDKShareKit/FBSDKShareKit/FBSDKSharing.h

您的SocialUtils对象可能会被垃圾收集。是否有任何其他对象强烈引用SocialUtils?如果没有,你可以这样做:

在YourAppDelegate上:

@property (nonatomic, strong) SocialUtils *socialUtils;

无论您在哪里定义SocialUtils:

SocialUtils *socialUtils = [[SocialUtils alloc] init];

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.socialUtils = socialUtils;

[socialUtils shareToFacebook:...]
// this method ^ would call [FBSDKShareAPI shareWithContent:content delegate:self];

它不必在AppDelegate上。它也可能在你的ViewController上,只要在这段时间内没有收集垃圾......