[英]Custom Delegate Method Not Being Called in Objective-C
A user is prompted to login in ViewController
. 提示用户登录ViewController
。 When the user clicks the login button, LoginService
's method loginWithUsername:andWithPassword
is called. 当用户单击登录按钮时,将LoginService
的方法loginWithUsername:andWithPassword
。 This method uses NSURLConnection
to fetch data from a web service and it authenticates the login credentials. 此方法使用NSURLConnection
从Web服务获取数据,并验证登录凭据。 It is an NSURLConnection
delegate (now I know there is another NSURLConnection method that uses block, I just prefer this one because I understand this, at least haha). 这是一个NSURLConnection
委托(现在我知道还有另一个使用block的NSURLConnection方法,我更喜欢这个方法,因为我了解这一点,至少是哈哈)。
I have set a protocol called LoginServiceProtocol
, with a delegate method called loginResult
which accepts an NSDictionary parameter loginData
. 我设置了一个名为LoginServiceProtocol
的协议,并使用一个名为loginResult
的委托方法,该方法接受一个NSDictionary参数loginData
。
When the NSURLConnection
delegate method connectionDidFinishLoading
is called, I want to call the method loginResult
and set its loginData
parameter to the data fetched from the website. 调用NSURLConnection
委托方法connectionDidFinishLoading
,我想调用方法loginResult
并将其loginData
参数设置为从网站获取的数据。
My problem is, I can't seem to be able to call the loginResult
method. 我的问题是,我似乎无法调用loginResult
方法。
To set the protocol, I have this code for LoginService.h: 要设置协议,我具有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;
This is my LoginService.m implementation in the connectionDidFinishLoading NSURLConnection delegate method: 这是在connectionDidFinishLoading NSURLConnection委托方法中的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];
}
}
I wrote [self loginResult:loginDict]
because without it, even the implementation of the delegate method in LoginService.m
isn't called. 我写了[self loginResult:loginDict]
因为没有它,就不会调用LoginService.m
中的委托方法的实现。 Without it, the program does nothing. 没有它,该程序将无法执行任何操作。 It does not show or do anything, even in my ViewController
which is a delegate of LoginService
. 它不显示或做任何事情,甚至在我ViewController
这是一个委托LoginService
。 The implementation of loginResult
in my LoginService.m
is just an NSLog()
checking if the method ever gets called. 我的LoginService.m
的loginResult
的实现只是一个NSLog()
用于检查是否曾经调用过该方法。
I know I'm missing a very vital thing, but I can't find it out. 我知道我错过了一件非常重要的事情,但我找不到。 I've been trying for quite a while now. 我已经尝试了一段时间了。 Also, I get this warning: 另外,我得到以下警告:
Authosynthesized property 'delegate' will use synthesised instance variable '_delegate', not existing instance variable 'delegate'.
I used the delegate in ViewController. 我在ViewController中使用了委托。 Here is my ViewController.h: 这是我的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
And I used it in the ViewController.m like this, just to see if it gets called: 我像这样在ViewController.m中使用它,只是看它是否被调用:
- (void)loginResult:(NSDictionary *)loginData{
NSLog(@"Reached");
}
I assigned it in the viewDidLoad: 我在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;
}
Most recent LoginService.h 最新的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
Most recent viewDidLoad implementation: 最新的viewDidLoad实现:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
self.ls.delegate=self; //Where ls is a strong reference of LoginService declared in .h
}
Remove: 去掉:
id<LoginServiceProtocol>delegate;
from here: 从这里:
@interface LoginService : NSObject<NSURLConnectionDataDelegate>{
NSMutableData* _responseData; //Response data catches the data received from the web api
id<LoginServiceProtocol>delegate;
}
您的控制器创建了LoginService
的实例,但不拥有它的所有权,因此该实例被释放在viewDidLoad
的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.