执行回调函数目标c

时间:2014-04-09 07:40:18

标签: ios objective-c ios7

对IOS开发不熟悉并且遇到以下问题,请帮忙。

我想执行回调函数,一旦showmessage函数完成其作业,就打印“hello world”

    [msgObj showMessage:@"hai how are you" autoClose:YES type:@"success" onCompletion:^(NSDictionary *str) {
    NSLog(@"hello world");
}];

以下两种方法存在于不同的文件

  -(void)showMessage:(NSString *)msg autoClose:(BOOL)close type:(NSString *)messageType onCompletion:(messageCompletionHandler) complete{
        [NSTimer scheduledTimerWithTimeInterval:3.0
                                           target:self
                                         selector:@selector(hideMessageinternal:)
                                         userInfo:complete
                                          repeats:NO];
}

致电

    -(void)hideMessageinternal:(void (^)(void))complete{
    complete(); // this is not calling the callback function to print hello world
}

1 个答案:

答案 0 :(得分:1)

你的hideMessageinternal:方法需要看起来像这样。

-(void)hideMessageinternal:(NSTimer *)timer
{
  void(^complete)() = timer[@"userInfo"]; // or [timer objectForKey:@"userInfo"];
  if (complete) {
    complete();
  }
}