【问题标题】:Delegate is not calling method代表没有调用方法
【发布时间】:2012-10-03 16:46:01
【问题描述】:

所以我有一个问题。 有人能告诉我这行代码调用(协议)方法的问题吗

 [self.delegate poplogin];

poplogin 是方法名称,由于某种原因它不起作用。 它没有调用方法 poplogin

供参考:

@property(nonatomic,retain) id<loginAuthDelegate> delegate;

让我解释一下这个案例

所以假设我有一个类 abc.h

@protocol loginAuthDelegate <NSObject>
-(void)poplogin;
@end

界面后

@property(nonatomic,retain) id<loginAuthDelegate> delegate;

in .m i am just calling the Delegate and @synthesize it
[self.delegate poplogin];

not i have another files
let say def.h
i am importing the Class abc.h
@interface def : UIViewController<loginAuthDelegate>



def.m
-(void)poplogin
{
NSLog(@"Delegate doesn't respond to here");
vmpaSecureLogin *ivc = [[vmpaSecureLogin alloc] init];
ivc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:ivc animated:YES];
}

【问题讨论】:

  • 你确定 delegate 不是 nil 吗?

标签: objective-c ios objective-c-protocol


【解决方案1】:

这可能是因为self.delegatenil

您可能忘记将对象的delegate 影响到您在其中实现了委托方法的其他对象,例如您的 ViewController 或其他东西。

【讨论】:

  • 所以你刚刚确认我解释的是你的问题的原因:在你在你的问题中写的代码中,你从来没有将委托属性分配给一个值(即abcInstance.delegate = defInstance;)所以当然delegate 属性的值是 nil — 并且向 nil 发送消息(如 [self.delegate poplogin]self.delegatenil 时)不会做任何事情
  • 能否请您建议在代码中的何处编写。或者如果你给我一个更好的例子
  • 我没有您创建 abc 实例的代码,无法猜测。但这与委托的每次使用(对于表格视图、文本视图、文本字段或每次使用委托)完全相同。当您创建类abc 的实例abcObject 时,可能在您的def 类的defObject 实例中,设置abcObject.delegate = defObject 以便def 对象成为abc 对象的代表并接收它的消息。这里没有魔法。
猜你喜欢
  • 2014-12-13
  • 1970-01-01
  • 2019-06-20
  • 2023-03-09
  • 2012-03-13
  • 2012-08-25
  • 2015-01-21
  • 1970-01-01
相关资源
最近更新 更多