我有一个可能要求用户在导航期间进行身份验证的应用。如果是这种情况,我希望登录事件将回调返回给请求登录事件的操作,以便知道登录是否成功。
例如,需要身份验证的方法将通过调用帮助程序类的方法来提示凭据:
[UserModel promptCredentials:^(NSHTTPURLResponse *response) {
if (response.statusCode == 200) {
[self userWillComment:userActionCell];
}
}];
helper方法实例化登录视图控制器,并尝试设置回调属性:
+ (void) promptCredentials:(void (^)(NSHTTPURLResponse *response))callback{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *currentViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
LoginViewController *loginScreenNav = [storyboard instantiateViewControllerWithIdentifier:@"LoginNavigationController"];
loginScreenNav.loginCallback = callback;
if([currentViewController isKindOfClass:[UINavigationController class]]){
currentViewController = [(UINavigationController *)currentViewController visibleViewController];
} else if(currentViewController.presentedViewController){
currentViewController = currentViewController.presentedViewController;
}
dispatch_async(dispatch_get_main_queue(), ^{
[currentViewController presentViewController:loginScreenNav animated:YES completion:nil];
});
}
Login视图控制器在其界面中定义了以下属性:
@property (nonatomic, strong) void (^loginCallback)(NSHTTPURLResponse *response);
当登录发生在'LoginViewController'中时,我试图执行回调,如下所示:
if (self.loginCallback != nil) {
self.loginCallback(response);
}
但是,当此代码运行时,帮助程序方法loginScreenNav.loginCallback = callback;
中的行会导致应用程序崩溃并显示错误:
- [UINavigationController setLoginCallback:]:无法识别的选择器发送到实例
基本上,我需要一些关于如何在登录视图控制器中正确分配此属性块的指导,以便在登录网络操作发生后执行它。
答案 0 :(得分:3)
崩溃的原因是你在这一行中欺骗自己(和编译器):
Also you have to make sure to set the background of the Frame as well in the OnActivated
您已从故事板中实例化了一个视图控制器,是的;但不是一个LoginViewController。它是一个UINavigationController。仔细检查您的故事板,查看标有LoginViewController *loginScreenNav = [storyboard instantiateViewControllerWithIdentifier:@"LoginNavigationController"];
标识符的视图控制器,您将看到这是真的。