等待另一个委托的目标c块

等待另一个委托的目标c块

问题描述:

我有一个watchkit应用程序,该应用程序在iphone应用程序上调用了一个viewcontroller.我有一个网络连接的代表.我正在尝试使用一个块,以便不会将AppDelegate和视图控制器紧密结合在一起.委托完成后如何通知我的座席?

I have a watchkit app that calls a viewcontroller on an iphone app. I have a delegate for a network connection. I'm trying to use a block so that I don't tightly couple my AppDelegate and my view controller too closely. How can I notify my block when the delegate is finished?

ViewController.m

ViewController.m

-(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{
   [self setUpAppForWatch];
   completion(YES);
}

-(void)finishedMessageParse:(NSMutableData *)messageData{
   //the delegate is finish tell the block completion is done.

}

-(void)setUpAppForWatch{
   [network call];
}

AppDelegate.m

AppDelegate.m

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)
(NSDictionary *))reply{

[vc getWatchDataWithCompletion:^(BOOL gotData){
    if (gotData){
       //I'm done reply dictionary
       reply(@{@"data":serlizedData})
}];

在viewcontroller中添加新属性:

add new property in viewcontroller:

@property (nonatomic, strong) void(^completion)(BOOL gotData);


-(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{
   [self setUpAppForWatch];
   self.completion = completion;
}

-(void)finishedMessageParse:(NSMutableData *)messageData{
    if (self.completion){
        self.completion(YES);
    }
}