0

クラスがUploadManagerあり、そのインスタンスを my に作成するとしますViewControllerUploadManager.m方法があります-(void)requestData

-(void)requestData
{
    HTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] init];
    [operation setCompletionBlockWithSuccess:^(HTTPRequestOperation *operation, id responseObject){
        // Do something here
    }];
    [operation start];
}

これでinrequestDataのインスタンスからメソッドを呼び出すことができますが、完了ブロックが起動したらの内部で何かをしたいと思います。これを行う最善の方法は何ですか?デリゲートメソッドを作成できると思いますが、より良い解決策があるかどうか疑問に思っています。ありがとう。UploadManagerViewController.mresponseObjectViewController.m

4

2 に答える 2

1

ブロックベースのアプローチは確かに機能します。ブロックへの別のアプローチが必要な場合はNSNotifications、次のように使用できます。

-(void)requestDataWithHandler:(void (^)(id responseObject))handler
{
    HTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] init];
    [operation setCompletionBlockWithSuccess:^(HTTPRequestOperation *operation, id responseObject){
        // You'll probably want to define the Notification name elsewhere instead of saying @"Information updated" below.
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Information updated" object:nil];
    }];
    [operation start];
}

の他の場所ViewController.m:

- (void)viewDidLoad
{
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingUponUpdate) name:@"Information updated" object:nil];
}

-(void)dealloc
{ 
  // Don't forget to remove the observer, or things will get unpleasant
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)doSomethingUponUpdate
{
  // Something
}
于 2014-12-17T22:18:19.370 に答える