English 中文(简体)
(c) 代表理解
原标题:objective c delegate understanding

我有一个类 ApiClient, 它从使用 NSURLCononnection 的服务器上获取一些数据, 并发回给它的代表 。

我还有另一个班级叫做Phicker.h, 它在 ApiClient 上调用一些函数, 并在其中执行代表方法 。

我有一个三等( View Control), 给 Fearcher. h 执行 Call () 打电话, 触发整个过程 。

    Fetcher *fetcher = [[Fetcher alloc] init];
    [fetcher getData];

如果我直接在视图控制器中调用 ApiClient 代码, 它会正常工作 。 为什么它不会在 Fearer 类中工作? 我做错了什么?

在Phicker内部得到Data 我有这个代码

APIClient* client = [APIClient sharedInstance];
[client setDelegate:self];
[client getData];

提前感谢。

最佳回答

由于您没有在查看控制器中为任何属性或任何东西指定 Fetcher 对象, 有可能您正在创建该对象, 它会在您分配它的任何方法的结尾处被释放。 请尝试将此添加到您的查看控制器 :

小时.h

@interface ViewController

@property (strong, nonatomic) Fetcher *myFetcher; //Add this line in your view controller s interface
...

时 时 时

@implementation ViewController

@synthesize myFetcher;
...
Fetcher *fetcher = [[Fetcher alloc] init];
[fetcher getData];
self时 时 时yFetcher = fetcher;//Add this line in your code as well
问题回答

试试这个...

@protocol APIClientProtocol <NSObject>
@optional
- (void) handleMessage;
@end

@interface APIClient : NSObject
@property (readwrite, nonatomic, strong) id<APIClientProtocol> delegate;
@end

@implementation APIClient
@synthesize delegate;

- (void) someAPIClientWork 
{

   // Do some client work here

   if ( [self.delegate respondsToSelector:@selector(handleMessage)] )
      [self.delegate performSelector:@selector(handleMessage)];
}

@end

.h 获取器

@interface Fetcher : NSObject <APIClientProtocol>
@end

.m 获取器

@implementation Fetcher

- (void) someInit 
{
   APIClient *client = [APIClient sharedInstance];
   client.delegate = self;
}

- (void) handleMessage 
{

   // Something in APIClient was called
}

@end




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签