gpt4 book ai didi

ios - 未在 Objective-C 中调用自定义委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-29 03:30:07 25 4
gpt4 key购买 nike

系统会提示用户登录ViewController。当用户单击登录按钮时,将调用 LoginService 的方法 loginWithUsername:andWithPassword。此方法使用 NSURLConnection 从 Web 服务获取数据并验证登录凭据。它是一个 NSURLConnection 委托(delegate)(现在我知道还有另一种使用 block 的 NSURLConnection 方法,我只是更喜欢这个,因为我理解这一点,至少哈哈)。

我设置了一个名为 LoginServiceProtocol 的协议(protocol),其中包含一个名为 loginResult 的委托(delegate)方法,该方法接受 NSDictionary 参数 loginData

当调用NSURLConnection委托(delegate)方法connectionDidFinishLoading时,我想调用方法loginResult并设置其loginData 从网站获取的数据的参数。

我的问题是,我似乎无法调用 loginResult 方法。

为了设置协议(protocol),我有 LoginService.h 的代码:

//To create a protocol
@protocol LoginServiceProtocol <NSObject>

-(void)loginResult:(NSDictionary*)loginData;

@end
//End of protocol creation

@interface LoginService : NSObject<NSURLConnectionDataDelegate>{
NSMutableData* _responseData; //Response data catches the data received from the web api
id<LoginServiceProtocol>delegate;
}

@property (nonatomic, assign) id <LoginServiceProtocol> delegate;

这是我在 connectionDidFinishLoading NSURLConnection 委托(delegate)方法中的 LoginService.m 实现:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSDictionary *loginDict = [NSJSONSerialization JSONObjectWithData:_responseData
options:NSJSONReadingMutableContainers
error:nil];
[self loginResult:loginDict];
if([self.delegate respondsToSelector:@selector(loginResult:)]){
[self.delegate loginResult:loginDict];
}
}

我编写了[self loginResult:loginDict],因为没有它,甚至不会调用LoginService.m中的委托(delegate)方法的实现。没有它,程序什么也不做。它不显示或执行任何操作,即使在我的作为 LoginService 委托(delegate)的 ViewController 中也是如此。我的 LoginService.mloginResult 的实现只是一个 NSLog() 检查该方法是否被调用。

我知道我错过了一件非常重要的事情,但我找不到它。我已经尝试了很长一段时间了。另外,我收到此警告:

Authosynthesized property 'delegate' will use synthesised instance variable '_delegate', not existing instance variable 'delegate'. 

我在 ViewController 中使用了委托(delegate)。这是我的 ViewController.h:

#import <UIKit/UIKit.h>
#import "LoginService.h"

@interface ViewController : UIViewController<LoginServiceProtocol>{

}

@property (weak, nonatomic) IBOutlet UITextField *txtUsername;
@property (weak, nonatomic) IBOutlet UITextField *txtPassword;
@property (weak, nonatomic) IBOutlet UILabel *lblName;

- (IBAction)btnLogin:(id)sender;
- (IBAction)btnKeyResign:(id)sender;
- (void)loginResult:(NSDictionary *)loginData;

@end

我像这样在 ViewController.m 中使用它,只是为了看看它是否被调用:

- (void)loginResult:(NSDictionary *)loginData{
NSLog(@"Reached");
}

我在viewDidLoad中分配了它:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.navigationController setNavigationBarHidden:YES];

LoginService* login = [[LoginService alloc]init];
login.delegate = self;

}

最新的LoginService.h

//Declaring custom delegate
@protocol LoginServiceProtocol <NSObject>

-(void)loginResult:(NSDictionary*)loginData;

@end


@interface LoginService : NSObject<NSURLConnectionDataDelegate>{
NSMutableData* _responseData;
id<LoginServiceProtocol>_delegate;
}
@property(nonatomic,strong)id<LoginServiceProtocol>delegate;
//End of custom delegate declaration

最近的 viewDidLoad 实现:

- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
self.ls.delegate=self; //Where ls is a strong reference of LoginService declared in .h

}

最佳答案

删除:

id<LoginServiceProtocol>delegate;

从这里开始:

@interface LoginService : NSObject<NSURLConnectionDataDelegate>{
NSMutableData* _responseData; //Response data catches the data received from the web api
id<LoginServiceProtocol>delegate;
}

关于ios - 未在 Objective-C 中调用自定义委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19910719/

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