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
}
3
  • 2
    Have a look at this. stackoverflow.com/questions/2651882/… You need to get the callback object from the userInfo. Commented Apr 9, 2014 at 7: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. Commented Apr 9, 2014 at 7:47
  • Passing a block and passing an object are the exact same thing, since blocks are objects. Commented Apr 9, 2014 at 8:17

1 Answer 1

1

Your hideMessageinternal: method needs to look like this.

-(void)hideMessageinternal:(NSTimer *)timer
{
  void(^complete)() = timer[@"userInfo"]; // or [timer objectForKey:@"userInfo"];
  if (complete) {
    complete();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.