URLから解析する別のクラス(NSXMLParse)を呼び出すクラスがあります。ここで、これを呼び出すクラスに、UIにデータを入力できるように、いつ終了したかを知らせたいと思います。代理人が行く方法だと思いますが、iveは代理人と一緒に仕事をしたことはなく、これをどのように配線するかについてのガイダンスが必要になります。
ありがとう
URLから解析する別のクラス(NSXMLParse)を呼び出すクラスがあります。ここで、これを呼び出すクラスに、UIにデータを入力できるように、いつ終了したかを知らせたいと思います。代理人が行く方法だと思いますが、iveは代理人と一緒に仕事をしたことはなく、これをどのように配線するかについてのガイダンスが必要になります。
ありがとう
NSNotificationは、私の意見では、このようなものを設定する最も簡単な方法です。
これは私のアプリの1つからの小さなスニペットです:
method.mで、処理中の処理が終了したとき
[[NSNotificationCenter defaultCenter] postNotificationName:@"GlobalTagsReady" object:nil];
通知の世話をするクラスで
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTags) name:@"GlobalTagsReady" object:nil];
@selector(getTags)
呼び出されるメソッドはどこですか
基本的に、委任とは、オブジェクトが何をしているのかを伝える必要がある何かへのポインタをオブジェクトに返すことを意味します。Cocoaでは、これは通常、インターフェイス宣言の逆である「プロトコル」を介して処理されます。これらは、プロトコルを「実装」する別のオブジェクトでオブジェクトが呼び出すメソッドを記述します。厳密に言えば、特にこのような単純な状況では必要ありませんが、モジュラーコードを作成するかどうかを知るには、良い習慣であり、良いことです。
解析クラスのヘッダー:
// This is the protocol declaration; anything implementing the "ParsingDelegate" protocol needs to have any method signatures listed herein
@protocol ParsingDelegate
- (void)parsingDidEndWithResult:(NSDictionary *)result
@end
@interface ParsingClass : NSObjectOrWhatever
{
...
id<ParsingDelegate> _delegate;
}
...
// We'll use this property to tell this object where to send its messages
@property(nonatomic,assign) id<ParsingDelegate> delegate;
@end
パーサーの実装では:
@implementation ParsingClass
@synthesize delegate=_delegate;
// the "=_delegate" bit isn't necessary if you've named the instance variable without the underscore
...
パーサーが処理を終了する方法では、次のようになります。
...
// make sure the delegate object responds to it
// assigning an object that doesn't conform to the delegate is a warning, not an error,
// but it'll throw a runtime exception if you call the method
if(self.delegate && [self.delegate respondsToSelector:@selector(parsingDidEndWithResult:)])
{
[self.delegate performSelector:@selector(parsingDidEndWithResult:) withObject:someResultObject];
}
...
UIクラスのヘッダー:
@interface SomeUIClass : NSObjectOrWhatever <ParsingDelegate>
UIクラスでは、パーサーを設定する場所はどこでも、
parser.delegate = self;
-parsingDidEndWithResult:
次に、UIクラスにメソッドを実装するだけです。一度に複数のパーサーを実行している場合は、デリゲートメソッドを拡張して、パーサーオブジェクト自体(Cocoa標準の一種)を渡すことができますが、説明した場合は、これでうまくいくはずです。
ココアで話す方法は主に 3 つあります。デリゲート、通知、KVO です。(参照http://alexvollmer.com/index.php/2009/06/24/cocoas-ways-of-talking/ )
バックグラウンド スレッドでデータを取得し、XML を解析していると仮定します。したがって、使用するソリューションが何であれ、メインスレッドで UI を更新することを忘れないでください (たとえば、performSelectorOnMainThread を使用)。