ios - 自定义委托(delegate)方法未在其他自定义类中使用其自定义委托(delegate)调用

标签 ios objective-c delegates

我有两个带有自定义委托(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/

相关文章:

objective-c - CTFrame 在边缘丢弃字形

ios - UITextField 返回键在 ViewController 中进行更改?

python - 使用 Django Rest Framework 和 IOS 应用程序时要使用哪种身份验证?

objective-c - FBSDKLog : System authorization failed:

objective-c - 基于 View 的 NSTableView 中的复选框问题

类似于 C++ 中的 C# 方法指针

javascript - jQuery 现场事件在 iPhone 4 上不起作用?

C#: ' += anEvent' 和 ' += new EventHandler(anEvent)' 之间的区别

c++ - FastDelegate 和 lambdas - 无法让它们工作(Don Clugston 最快的委托(delegate))

objective-c - 为什么我在基于文档的应用程序中收到无效上下文 0x0 错误?