xcode 委托对象没有调用协议方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9677221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
protocol method is not being called by the delegate object
提问by TommyG
Another case of protocol method not being called - NO idea what am I doing wrong here...
未调用协议方法的另一种情况 - 不知道我在这里做错了什么......
Here is the code, omitting unnecessary info...
这是代码,省略了不必要的信息......
first header file: AccessNumber.h
第一个头文件:AccessNumber.h
@protocol AddItemDelegate <NSObject>
@required
- (void) processSuccessful: (BOOL)success;
@end
@interface AccessNumber : UIViewController <UITableViewDelegate, UITableViewDataSource, ABPeoplePickerNavigationControllerDelegate, UIAlertViewDelegate> {
id <AddItemDelegate> delegate;
}
@property (retain) id <AddItemDelegate> delegate;
@end
first .m file: AccessNumber.m - I am calling the protocol method from viewdidload just for testing purposes, it should basically get called from another method, but same thing for the sake of this convo (tried both of course)
第一个 .m 文件:AccessNumber.m - 我从 viewdidload 调用协议方法只是为了测试目的,它基本上应该从另一个方法调用,但为了这个 convo 也是一样的(当然都试过)
#import "AccessNumber.h"
#import "History.h"
@synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
....
[[self delegate] processSuccessful:YES];
}
second file header: History.h
第二个文件头:History.h
@interface History : UIViewController <UITableViewDelegate, UITableViewDataSource, AddItemDelegate> {
....
}
method implementation in history.m
history.m中的方法实现
- (void) processSuccessful: (BOOL)success {
NSLog(@"SUCCESS");
}
Appreciate any help. Thanks!
感谢任何帮助。谢谢!
回答by LuisEspinoza
In the code i don't see something like:
在代码中我没有看到类似的东西:
theAccessNumber.delegate = self;
You must set the History instance as the delegate property of the AccessNumber instance.
您必须将 History 实例设置为 AccessNumber 实例的委托属性。
Regards.
问候。
回答by Pochi
The code seems a little bit funny but you are not telling your "AccessNumber" class who is his delegate, even though you are making the "History" class implement the protocol established by yourself (otherwise you would get a warning).
代码看起来有点滑稽,但你没有告诉你的“AccessNumber”类谁是他的代表,即使你让“History”类实现你自己建立的协议(否则你会收到警告)。
You have to set the delegate for the "AccessNumber" class like this when setting it up from within "AccessNumber.m":
从“AccessNumber.m”中设置“AccessNumber”类时,您必须像这样设置“AccessNumber”类的委托:
self.delegate = historyInstance;
or like this when setting it up from within "History.m":
或者在“History.m”中设置时像这样:
accessNumberInstance.delegate = self;
回答by fbernardo
There are very vew situations where you should retain a delegate, normally your delagate will outlive the object. So change your property from retain to assign. And be sure you are setting the delegate. Where are you doing it? If your object really depends on it you should be passing it in the constructor (iniWithDelagate). Try doing a NSLog before calling the delagate method just to see if it isn't nil.
在某些情况下,您应该保留委托,通常您的委托将比对象更长寿。因此,将您的财产从保留更改为分配。并确保您正在设置委托。你在哪里做?如果您的对象确实依赖于它,您应该将它传递到构造函数 (iniWithDelagate) 中。在调用 delagate 方法之前尝试做一个 NSLog,看看它是否不是 nil。