我有两个带有自定义委托(delegate)的类。第一类有一个在 View Controller 中正确调用的必需方法。当我尝试在具有自己的委托(delegate)的第二个类中使用第一个类的委托(delegate)方法时,没有任何反应。我看到从未调用过委托(delegate)方法。下面是这两个类的类接口(interface)声明: A 级:
@class ServerCommunication;
@protocol ServerCommunicationDelegate;
@interface ServerCommunication : NSObject
@property (weak, nonatomic) id <ServerCommunicationDelegate> delegate;
-(void)requestPageXMLForURLString:(NSString *)stringURL;
-(void)getPreviewBlastsListXMLFromBlastsListItems:(NSString *)blastsIdsString;
@end
@protocol ServerCommunicationDelegate <NSObject>
@required
- (void)didFinishUIXMLRequestWithSuccess:(NSString *)responseXMLString;
@end
B 级:
@class AnnotationsListViews;
@protocol AnnotationsListViewsDelegate;
@interface AnnotationsListViews : NSObject<ServerCommunicationDelegate>
{
GDataXMLDocument *listViewXML;
}
@property (weak, nonatomic) id <AnnotationsListViewsDelegate> delegate;
@property (strong, nonatomic) NSString *blastsIds;
- (id)initWithBlastsIds:(NSString *)blastsIdsString;
-(void)getPreviewBlastsListXMLFromBlastsListItems:(NSString *)blastsIdsString;
@end
@protocol AnnotationsListViewsDelegate <NSObject>
@required
-(void)selectedAnnotationsListTitleText:(NSString *)listTitle;
-(void)annotationsListViewsGeneratedWithViewsArray:(NSArray *)requestedBlastsListItemsViews;
@结束 这是 B 类 .m 文件中调用第一类委托(delegate)方法的方法:
-(void)getXML:(NSString *)blastsIdsString{
ServerCommunication *connectionToServer=[[ServerCommunication alloc] init];
connectionToServer.delegate=self;
[connectionToServer getPreviewBlastsListXMLFromBlastsListItems:blastsIdsString];
}
代码的最后一行工作正常,从头等舱调用“getPreviewBlastsListXMLFromBlastsListItems”。执行第二类中的每一行代码。但是不会调用跟随委托(delegate)方法。 要调用的委托(delegate):
#pragma mark - ServerCommunicationDelegate
- (void)didFinishUIXMLRequestWithSuccess:(NSString *)responseXMLString{
NSLog(@"sasasassasasasasasasaa");
}
相同的调用方法和委托(delegate)方法,我一直在我所有的 View Controller 类中使用它们,它们工作正常。在这种情况下,我看到甚至 NSLog 不打印任何东西。我认为在从 NSObject 类继承的类中有一些单独的方法来处理调用委托(delegate)方法。我该如何解决?
最佳答案
将ServerCommunication *connectionToServer
设为@property
,例如
@property (strong, nonatomic) ServerCommunication *connectionToServer;
并将getXML:
方法更改为
-(void)getXML:(NSString *)blastsIdsString{
self.connectionToServer=[[ServerCommunication alloc] init];
self.connectionToServer.delegate=self;
[self.connectionToServer getPreviewBlastsListXMLFromBlastsListItems:blastsIdsString];
}
同时创建 B 类的实例,即。 AnnotationsListViews
到 @property
。
关于ios - 自定义委托(delegate)方法未在其他自定义类中使用其自定义委托(delegate)调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25742476/