【问题标题】:Custom Delegate Method Not Being Called in Objective-C在 Objective-C 中未调用自定义委托方法
【发布时间】:2013-11-11 16:04:05
【问题描述】:

提示用户登录ViewController。当用户点击登录按钮时,LoginService的方法loginWithUsername:andWithPassword被调用。此方法使用NSURLConnection 从 Web 服务获取数据并验证登录凭据。它是一个NSURLConnection 委托(现在我知道还有另一种使用块的 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 的代表。 loginResult 在我的LoginService.m 中的实现只是一个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

}

【问题讨论】:

  • 警告显示在哪里?
  • @nhgrif 在 LoginService.m 文件中的 @implementation LoginService
  • 在你的实现文件中包含@synthesize delegate

标签: ios objective-c delegates


【解决方案1】:

删除:

id<LoginServiceProtocol>delegate;

从这里:

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

【讨论】:

  • 它摆脱了警告,但仍然没有调用 LoginService.m 中的委托方法 :( 我的意思是,没有 [self loginResult]。即使在 ViewControllerloginResult方法没有被调用。
  • 你在委托中实现 loginResult 了吗?
  • 是的。但是我现在只有 NSLog 来查看方法是否被调用,它不是……..
  • 我可以看看你指派代表的位置吗?
  • 你如何分配它?
【解决方案2】:

您的控制器创建了一个LoginService 的实例,但没有获得它的所有权,因此该实例在viewDidLoad 结束时被释放。

【讨论】:

  • strong 属性添加到您的视图控制器类,并将对LoginService 实例的引用存储在那里而不是本地变量中。 (请注意,即使您一开始就这样做了,由于@AntonioMG 指出的错误,您的实现仍然会被破坏)。
  • 我已经这样做了。但我想我刚刚发现了我的问题。代表为零。我希望你,先生,可以帮助我解决它。我将使用我最近的协议声明来更新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多