【问题标题】:Delegate methods not called when being hidden隐藏时未调用委托方法
【发布时间】:2013-09-06 04:58:15
【问题描述】:

我有一个视图控制器,它从我的 facebook 助手中实现了一些委托方法。当点击某个图标(如图标)时,我的应用会切换到 facebook 移动网络进行登录。之后,它切换回我的应用程序,但是,该应用程序需要更多的写入权限,因此 facebook 移动网络(safari)再次切换。我按确定,操作完成。 但是,我的视图控制器中的委托方法没有被调用。我调试并可以看到当时发生了 facebook 操作,变量“delegate”为 nil。 我的视图控制器有一些特别之处。每次从后台返回时,它都会呈现另一个视图控制器(用于赞助商屏幕)。显示持续时间大约为 3 秒,然后赞助商屏幕消失。
我怎样才能调用委托方法?
在我的视图控制器中
[FacebookHelper sharedInstance].fbDelegate = self; [[FacebookHelper sharedInstance] likeCommentFacebook:!isLike withCommentID:message.messageID];
在我的 Facebook 助手中:
`- (void) likeCommentFacebook:(BOOL)isLike withCommentID:(NSString *)commentID {

//- check if we are already processed by the loginCallback
if (_loginCallbackInvocation == nil) {
    //- create NSInvocation reference to this method
    NSInvocation * nsi = [WKTools invocationWithTarget:self selector:_cmd retainArguments:TRUE argumentAddresses:&isLike, &commentID];
    //- ensure user has been properly identified and process with authentication if required
    [self processLogin:nsi];
}

//- check if user session is valid
if ([[FBSession activeSession] isOpen]) {
    if (![self hasPermission:kPublishStream]) {
        NSArray *permissions = [NSArray arrayWithObjects: kPublishStream,nil];
        dispatch_async(dispatch_get_current_queue(), ^{
            [FBSession.activeSession requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone completionHandler:^(FBSession *session, NSError *error) {
                NSString *requestUrl = [NSString stringWithFormat:@"%@%@", commentID,FB_REQUEST_LIKE];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"", MESSAGE_KEY, nil];
                //- send request
                if (isLike == YES) {
                    [self requestGraphAPI:requestUrl parameter:params httpMethod:POST_METHOD];
                } else {
                    [self requestGraphAPI:requestUrl parameter:params httpMethod:DELETE_METHOD];
                }
            }];
        });
    } else {
        NSString *requestUrl = [NSString stringWithFormat:@"%@%@", commentID,FB_REQUEST_LIKE];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"", MESSAGE_KEY, nil];
        //- send request
        if (isLike == YES) {
            [self requestGraphAPI:requestUrl parameter:params httpMethod:POST_METHOD];
        } else {
            [self requestGraphAPI:requestUrl parameter:params httpMethod:DELETE_METHOD];
        }
    }
} else {
    if ([self isSessionCachedAndLoaded]) {
        [FBSession.activeSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            if (![self hasPermission:kPublishStream]) {
                [[NSUserDefaults standardUserDefaults] synchronize];
                NSArray *permissions = [NSArray arrayWithObjects: kPublishStream,nil];
                dispatch_async(dispatch_get_current_queue(), ^{
                    [FBSession.activeSession requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone completionHandler:^(FBSession *session, NSError *error) {
                        NSString *requestUrl = [NSString stringWithFormat:@"%@%@", commentID,FB_REQUEST_LIKE];
                        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"", MESSAGE_KEY, nil];
                        DEBUGLog(@"like comment: %@",requestUrl);

                        //- send request
                        if (isLike == YES) {
                            [self requestGraphAPI:requestUrl parameter:params httpMethod:POST_METHOD];
                        } else {
                            [self requestGraphAPI:requestUrl parameter:params httpMethod:DELETE_METHOD];
                        }
                    }];
                });
            } else {
                NSString *requestUrl = [NSString stringWithFormat:@"%@%@", commentID,FB_REQUEST_LIKE];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"", MESSAGE_KEY, nil];
                //- send request
                if (isLike == YES) {
                    [self requestGraphAPI:requestUrl parameter:params httpMethod:POST_METHOD];
                } else {
                    [self requestGraphAPI:requestUrl parameter:params httpMethod:DELETE_METHOD];
                }
            }
        }];
    }
}

}`

在回调方法中


if (self.fbDelegate && [self.fbDelegate respondsToSelector:@selector(likeCommentDidSuccess:)]) { [self.fbDelegate performSelector:@selector(likeCommentDidSuccess:) withObject:dictionary]; }

【问题讨论】:

  • 看不到代码我们就忍不住了。
  • 发布代码很困难,因为流程要经过许多方法和块。我检测到的另一件事是,如果我使用 NSNotificationCenter 然后将通知与我的 ViewController 中的方法挂钩,该方法可以接收返回的信号。
  • 不确定我们能做什么。代表为什么要去nil,这纯属猜测。
  • @trojanfoe:请看上面的代码

标签: iphone ios objective-c


【解决方案1】:

您是否在该ViewController 中将delegate 设置为self,并且还提到/实现了yourViewController 的.h 文件中的委托,如下所示。

yourViewController.h

@interface yourViewController : UIViewController<yourDelegate>

yourViewController.m

[yourComponent setYourDelegate:self];

【讨论】:

  • 当然,这是我为我的服务助手设置的第一件事。
  • 啊,终于找到原因了。在 viewWillDisappear 方法中,我设置了 delegate = nil 以避免挂起的请求崩溃。因为我有一个模态视图控制器总是出现在我的视图控制器上方,所以该方法将被称为使委托为零。
猜你喜欢
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 2015-06-09
  • 2016-07-16
  • 2019-01-04
相关资源
最近更新 更多