29,028
社区成员
发帖
与我相关
我的任务
分享
- (void)doSomethingWithBlock:(NSString *(^)())block {
NSLog(@"callback:%@", block());
}
- (void)callMethod {
__weak typeof(self) weakSelf = self;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(queue, ^{
[self doSomethingWithBlock:^NSString *{
__block NSString *resultString = nil;
__strong typeof(weakSelf) strongSelf = weakSelf;
// 信号量
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message..." preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:nil];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 获取弹框输入值
resultString = [[alertController.textFields objectAtIndex:0] text];
}]];
[strongSelf presentViewController:alertController animated:YES completion:^{
// 完成信号
dispatch_semaphore_signal(sema);
}];
// 等待
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
return resultString;
}];
});
}