为FFStatusDisplay单例的状态显示方法添加Continuation Block
Got it, let's add that continuation block to your showStatus:Timeout:Controller: method step by step. Here's how you can modify your code to support a completion handler that runs after the status alert dismisses:
Adding a Continuation Block to
FFStatusDisplay's Status Method First, update the method signature to include an optional completion block parameter. We'll make it nullable so callers aren't forced to use it:
// In your FFStatusDisplay.h file - (void)showStatus:(NSString *)message Timeout:(float)timeout Controller:(UIViewController *)controller Completion:(void (^_Nullable)(void))completion;
Then, modify the implementation to store and execute the block once the alert finishes dismissing. We'll also handle the case where an alert is already being displayed:
// In your FFStatusDisplay.m file - (void)showStatus:(NSString *)message Timeout:(float)timeout Controller:(UIViewController *)controller Completion:(void (^_Nullable)(void))completion { if (![self isDisplaying]) { [self setIsDisplaying:YES]; // Finish creating your UIAlertController (your existing code here) UIAlertController *statusAlert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert]; [self setStatusAlert:statusAlert]; [controller presentViewController:statusAlert animated:YES completion:nil]; // Schedule dismissal and completion block execution dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [statusAlert dismissViewControllerAnimated:YES completion:^{ [self setIsDisplaying:NO]; [self setStatusAlert:nil]; // Run the continuation block if it was provided if (completion) { completion(); } }]; }); } else { // Handle the case where an alert is already showing // For this example, we'll immediately run the completion block // (you could instead update the existing alert's message/timeout if needed) if (completion) { completion(); } } }
Key Details:
- The completion block runs after the alert is fully dismissed, using the dismissal's own completion handler to ensure UI state is updated first.
- Making the block nullable means existing calls to the method won't break—callers can choose to add the continuation only when needed.
- The "already displaying" logic is flexible: you can adjust it to update the current alert instead of ignoring the request, based on your app's needs.
Example Usage:
[[FFStatusDisplay sharedInstance] showStatus:@"Task finished successfully!" Timeout:2.5 Controller:self Completion:^{ // This code executes right after the alert disappears NSLog(@"Continuing with next operation..."); [self loadNextScreen]; }];
内容的提问来源于stack exchange,提问作者David U
相关产品推荐
相关产品推荐

