ios - 当 self 等于 NSObject 时委托(delegate)丢失

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

我正在使用 [FBSDKShareAPI shareWithContent:content delegate:self]; 在 facebook 中分享一张 photo 并且 self 等于 NSObject称为 SocialUtils。

我直接去图书馆放日志。

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)

我尝试了同样的方法,但这次 self 等于 UIViewController 并且工作完美,委托(delegate)仍然存在。

我不明白为什么会这样,我需要有单独的 Controller 逻辑,否则将意味着在许多 UIViewController 中重复代码。

有没有人能赐教一下,是因为出现这种情况吗?

最佳答案

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 上,只要在此期间没有被垃圾收集...

关于ios - 当 self 等于 NSObject 时委托(delegate)丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36507879/

相关文章:

ios - (Swift Playground Template) 使用 Swift 5.1 编译的模块无法被 Swift 5.1.3 编译器导入

iphone - viewDidLoad 中的异常 animateWithDuration 行为

ios - objective-C : UIImagePickerControllerReferenceURL get ID

c# - 如果我不再关心结果,是否还有结束委托(delegate)的方法?

ios - 标签显示 html 数据并使图像和链接可快速点击

ios - 为什么 detailViewController 不再在 UISplitViewController.viewControllers 中了?

ios - 从 NSString 中删除所有非英文字符

iphone - 使用 NSOperationQueue 在单独的线程上创建自动释放的对象

Swift - 为什么 NSOutlineView 中的参数项总是 nil?

javascript - JavaScript 元编程的动态参数?