0

Am new to IOS development and am stuck with the below issue, please help.

I want to execute the callback function which prints "hello world" once the showmessage function is done with its job

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

the below two methods exists under different file

  -(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];
}

Calling

    -(void)hideMessageinternal:(void (^)(void))complete{
    complete(); // this is not calling the callback function to print hello world
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Navin Leon
  • 1,136
  • 3
  • 22
  • 44
  • 2
    Have a look at this. http://stackoverflow.com/questions/2651882/passing-data-through-nstimer-userinfo You need to get the callback object from the userInfo. – sbarow Apr 09 '14 at 07:43
  • Hi sbarow, the link you provided have information on how to pass data but no information available on how to pass a function and execute a callback function. – Navin Leon Apr 09 '14 at 07:47
  • Passing a block and passing an object are the exact same thing, since blocks *are* objects. – borrrden Apr 09 '14 at 08:17

1 Answers1

1

Your hideMessageinternal: method needs to look like this.

-(void)hideMessageinternal:(NSTimer *)timer
{
  void(^complete)() = timer[@"userInfo"]; // or [timer objectForKey:@"userInfo"];
  if (complete) {
    complete();
  }
}
sbarow
  • 2,759
  • 1
  • 22
  • 37