关于ios:执行回调函数Objective C

Execute a callback function objective c

我是IOS开发的新手,并且遇到了以下问题,请提供帮助。

我想在showmessage函数完成其工作后执行打印" hello world "的回调函数。

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

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

1
2
3
4
5
6
7
  -(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];
}

呼叫

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


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

1
2
3
4
5
6
7
-(void)hideMessageinternal:(NSTimer *)timer
{
  void(^complete)() = timer[@"userInfo"]; // or [timer objectForKey:@"userInfo"];
  if (complete) {
    complete();
  }
}