簡體   English   中英

在Objective-C中未調用自定義委托方法

[英]Custom Delegate Method Not Being Called in Objective-C

提示用戶登錄ViewController 當用戶單擊登錄按鈕時,將LoginService的方法loginWithUsername:andWithPassword 此方法使用NSURLConnection從Web服務獲取數據,並驗證登錄憑據。 這是一個NSURLConnection委托(現在我知道還有另一個使用block的NSURLConnection方法,我更喜歡這個方法,因為我了解這一點,至少是哈哈)。

我設置了一個名為LoginServiceProtocol的協議,並使用一個名為loginResult的委托方法,該方法接受一個NSDictionary參數loginData

調用NSURLConnection委托方法connectionDidFinishLoading ,我想調用方法loginResult並將其loginData參數設置為從網站獲取的數據。

我的問題是,我似乎無法調用loginResult方法。

要設置協議,我具有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委托方法中的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中的委托方法的實現。 沒有它,該程序將無法執行任何操作。 它不顯示或做任何事情,甚至在我ViewController這是一個委托LoginService 我的LoginService.mloginResult的實現只是一個NSLog()用於檢查是否曾經調用過該方法。

我知道我錯過了一件非常重要的事情,但我找不到。 我已經嘗試了一段時間了。 另外,我得到以下警告:

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

我在ViewController中使用了委托。 這是我的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;
}

您的控制器創建了LoginService的實例,但不擁有它的所有權,因此該實例被釋放在viewDidLoad的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM