在解除ModalViewController后刷新父ViewController [英] Refreshing Parent ViewController after dismissing ModalViewController
问题描述
在我的iOS应用程序中,用户可以从列表中选择一个图像,在该图像上显示一个包含图像的模式和删除图像的选项。如果用户选择删除图像,则返回到包含图像列表的原始viewController。我需要刷新原始ViewController以考虑已删除的图像。
In my iOS app, a user can select an image from a list, upon which they are presented with a modal that contains the image and options to delete the image. If the user chooses to delete the image, she is returned to the original viewController containing the list of images. I need to then refresh the original ViewController to take into account the deleted image.
我尝试使用NSNotificationCenter在将图像删除到父视图控制器时进行广播。但是,似乎从未收到广播。
I tried using NSNotificationCenter to broadcast when an image is deleted to the parent View Controller. However, it seems like the broadcast is never received.
是否有其他方式
- 将数据发送回父ViewController在模态被解除之后,
- 检测何时从父ViewController中解除模态?
(我尝试按照概述这里,但它似乎不起作用)
(I tried following the example outlined here, but it didn't seem to work)
以下是我的代码:
EditStepViewController(原始视图控制器):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MediaPreviewViewController *mediaPreviewVC = (MediaPreviewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MediaPreviewViewController"];
mediaPreviewVC.selectedImageURL = [NSString stringWithFormat:@"%@",gestureRecognizer.view.tag];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mediaPreviewVC];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didDismissMediaPreview)
name:@"MediaPreviewDismissed"
object:nil];
[self presentViewController:navigationController animated:YES completion:nil];
MediaPreviewViewController(第二个ViewController):
...
[self deleteImage];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MediaPreviewDismissed" object:nil userInfo:nil];
[self dismissViewControllerAnimated:YES completion:^(){
NSLog(@"dismissed controller");
}];
然后,回到EditStepViewController:
Then, back in EditStepViewController:
-(void)didDismissMediaPreview{
NSLog(@"dismissed media preview"); // this is never logged!
[self.view setNeedsDisplay]; // refresh view to account for deleted image
}
预先感谢您的帮助!
推荐答案
在我的情况下,我通常在这里使用块。
In my case I usually use block here.
For示例你有 ParentViewController.h
@interface ParentViewController : UIViewController
@end
实施 ParentViewController.m
// INCLUDE HERE THE MODAL CONTROLLER TO HAVE ACCESS TO ITS PUBLIC PROPERTY
#import ModalViewController.h
@implementation ParentViewController
// implement your modal dismiss block here
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// DEFINE HERE THE CALLBACK FUNCTION
// 1. get the model view controller
ModalViewController *mvc = [segue destinationViewController];
// 2. Your code after the modal view dismisses
mvc.onDismiss = ^(UIViewController *sender, NSObject *objectFromModalViewController)
{
// Do your stuff after dismissing the modal view controller
.
.
.
}
}
@end
并且, ModalViewController.h
@interface ModalViewController : UIViewController
// call back function, a block
@property (nonatomic, strong) void (^onDismiss)(UIViewController *sender, NSObject *objectYouWantToPassBackToParentController)
@end
ModalViewController.m
@implementation ModalViewController
.
.
.
// your dismiss function say
- (IBAction)dismissViewController:(id)sender
{
...
[self deleteImage];
[self dismissViewControllerAnimated:YES completion:^
{
// MAKE THIS CALL
self.onDismiss(self, theOjectYouWantToPassBackToParentVC);
}];
}
@end
这篇关于在解除ModalViewController后刷新父ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!