sci*_*fic 11 objective-c uiviewcontroller nsnotificationcenter ios presentviewcontroller
在我的iOS应用程序中,用户可以从列表中选择一个图像,在该图像上显示一个包含图像的模态和删除图像的选项.如果用户选择删除图像,则返回到包含图像列表的原始viewController.我需要刷新原始ViewController以考虑已删除的图像.
当图像被删除到父视图控制器时,我尝试使用NSNotificationCenter进行广播.但是,似乎从未收到广播.
还有其他方法吗?
(我试着按照这里列出的例子,但它似乎没有工作)
以下是我的代码:
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];
Run Code Online (Sandbox Code Playgroud)
MediaPreviewViewController(第二个ViewController):
...
[self deleteImage];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MediaPreviewDismissed" object:nil userInfo:nil];
[self dismissViewControllerAnimated:YES completion:^(){
NSLog(@"dismissed controller");
}];
Run Code Online (Sandbox Code Playgroud)
然后,回到EditStepViewController:
-(void)didDismissMediaPreview{
NSLog(@"dismissed media preview"); // this is never logged!
[self.view setNeedsDisplay]; // refresh view to account for deleted image
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助!
All*_*rao 19
在我的情况下,我通常在这里使用块.
例如,您有ParentViewController.h
@interface ParentViewController : UIViewController
@end
Run Code Online (Sandbox Code Playgroud)
实现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
Run Code Online (Sandbox Code Playgroud)
而且,ModalViewController.h
@interface ModalViewController : UIViewController
// call back function, a block
@property (nonatomic, strong) void (^onDismiss)(UIViewController *sender, NSObject *objectYouWantToPassBackToParentController)
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7994 次 |
| 最近记录: |