问题是并非所有委托方法都被调用。
Login.h
#import <Foundation/Foundation.h>
@protocol LTLoginDelegate;
@interface LTLogin : NSObject
@property(nonatomic, weak) id <LTLoginDelegate> delegate;
-(void)commitLogin:(NSString*)login password:(NSString*)password;
-(void)login:(LTLogin*)login didBecomeActive:(BOOL)success;
-(void)login:(LTLogin*)login didFailWithError:(NSError*)error;
-(void)login:(LTLogin*)login didSuccessWithMessage:(NSString*)message;
-(void)storeLoginCredentials;
@end
@protocol LTLoginDelegate <NSObject>
@optional
-(void)storeLoginCredentials;
-(void)login:(LTLogin*)login didSuccessStoringCredentials:(BOOL)success;
-(void)login:(LTLogin*)login didFailStoringCredentials:(BOOL)success;
@required
-(void)login:(LTLogin*)login didBecomeActive:(BOOL)success;
-(void)login:(LTLogin*)login didFailWithError:(NSError*)error;
-(void)login:(LTLogin*)login didSuccessWithMessage:(NSString*)message;
@end
Login.m
@implementation LTLogin
-(void)commitLogin:(NSString *)login password:(NSString *)password {
// >>> ....
if([self.delegate respondsToSelector:@selector(login:didBecomeActive:)]) {
[self.delegate login:self didBecomeActive:YES];
}
}
-(void)login:(LTLogin *)login didBecomeActive:(BOOL)success {
// >>> ....
// Don't get called here at all!!!!!!
[self.delegate login:self didSuccessWithMessage:@"Success"];
}
-(void)login:(LTLogin *)login didSuccessWithMessage:(NSString *)message {
}
@end
Controller.m或者
-(void)viewDidAppear:(BOOL)animated {
#if LOG == 0
NSLog(@"This one");
#endif
LTLogin *login = [[LTLogin alloc]init];
login.delegate = self;
[login commitLogin:@"john.doe@hmail.con" password:@"123123"];
}
-(void)login:(LTLogin *)login didFailWithError:(NSError *)error {
#if LOG == 1
NSLog(@"login didFailWithError: Error %@",error);
#endif
}
-(void)login:(LTLogin *)login didSuccessWithMessage:(NSString *)message {
#if LOG == 1
NSLog(@"login didSucceedWithMessage: %@",message);
#endif
}
-(void)login:(LTLogin *)login didBecomeActive:(BOOL)success {
#if LOG == 1
NSLog(@"login didBecomeActive");
#endif
// Gets called here only!!!!!!
}
在登录控制器上没有调用didBecomeActive。我的目标是构建一系列相互调用的回调方法。
答案 0 :(得分:1)
仅在当前委托上调用方法-(void)login:(LTLogin *)login didBecomeActive:(BOOL)success
。在-(void)viewDidAppear:(BOOL)animated
中,您将其设置为Controller
。如果您想拥有多个代理人,可以使用observer pattern或NSNotificationCenter
。请记住添加控制器和LTLogin实例。
答案 1 :(得分:1)
问题在于呼叫者&#34; self.delegate&#34;自我&#34;。用
替换旧版本的commitLogin-(void)commitLogin:(NSString *)login password:(NSString *)password {
// >>> ....
if([self.delegate respondsToSelector:@selector(login:didBecomeActive:)]) {
[self login:self didBecomeActive:YES];
}
}