2014-04-09 3 views
0

IOS 개발에 익숙하지 않고 아래 문제로 해결되지 않았습니다. 도와주세요.콜백 함수 목표를 실행하십시오.

난에 "Hello World"를 showmessage 기능을 한번의 작업으로 이루어집니다 인쇄 콜백 기능을 실행해야합니다

두 가지 방법 아래
[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 
} 
+2

이것 좀보세요. http://stackoverflow.com/questions/2651882/passing-data-through-nstimer-userinfo userInfo에서 콜백 객체를 가져와야합니다. – sbarow

+0

안녕하세요, 귀하가 제공 한 링크는 데이터를 전달하는 방법에 대한 정보가 있지만 함수를 전달하고 콜백 함수를 실행하는 방법에 대한 정보는 없습니다. –

+0

블록을 전달하고 객체를 전달하는 것은 블록 *이 * 객체이므로 완전히 똑같습니다. – borrrden

답변

1

hideMessageinternal : 메소드는 다음과 같이 표시되어야합니다.

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