Make UIAlertView blocking

后端 未结 5 2059
刺人心
刺人心 2020-12-03 12:49

I need make UIAlertView blocking. Because i have function and i need to return UIAlertView choice. But problem is that after UIAlertView

5条回答
  •  孤街浪徒
    2020-12-03 13:03

    Use the UIAlertView with blocks from Joseph and add a semaphore to it. Declare a global semaphore

    dispatch_semaphore_t generateNotificationsSemaphore;
    

    And signal the semaphore in the block handler

    [alert showWithHandler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == [alertView cancelButtonIndex]) {
    
        } else {
    
        }
    
        dispatch_semaphore_signal(generateNotificationsSemaphore);
    }];
    

    After calling the showWithHandler add a waiting loop using the semaphore

    while (dispatch_semaphore_wait(generateNotificationsSemaphore, DISPATCH_TIME_NOW )) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:20]];
    
    }
    

    Your actual timeout value may be different depending on your needs.

提交回复
热议问题