gpt4 book ai didi

iOS:不会调用委托(delegate)回调

转载 作者:行者123 更新时间:2023-11-29 10:22:08 25 4
gpt4 key购买 nike

问题是并不是所有的委托(delegate)方法都会被调用。

登录.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

登录.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 未在登录 Controller 上调用。我的目标是构建一个相互调用的回调方法链。

最佳答案

问题是调用者“self.delegate”到“self”。将旧版本的 commitLogin 替换为

-(void)commitLogin:(NSString *)login password:(NSString *)password {

// >>> ....
if([self.delegate respondsToSelector:@selector(login:didBecomeActive:)]) {

[self login:self didBecomeActive:YES];
}
}

关于iOS:不会调用委托(delegate)回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34491114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com