11

在我的 iOS 应用程序中,用户可以从列表中选择一个图像,然后他们会看到一个包含图像和删除图像选项的模式。如果用户选择删除图像,她将返回到包含图像列表的原始 viewController。然后我需要刷新原始 ViewController 以考虑删除的图像。

当图像被删除到父视图控制器时,我尝试使用 NSNotificationCenter 进行广播。但是,似乎从未收到广播。

有没有其他方法可以

  1. 模式关闭后将数据发送回父 ViewController,并且
  2. 检测模式何时从父 ViewController 中消失?

(我尝试按照此处概述的示例进行操作,但似乎没有用)

下面是我的代码:

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:

-(void)didDismissMediaPreview{
    NSLog(@"dismissed media preview"); // this is never logged!
    [self.view setNeedsDisplay]; // refresh view to account for deleted image
}

在此先感谢您的帮助!

4

3 回答 3

19

就我而言,我通常在这里使用块。

例如你有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

模态视图控制器.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
于 2014-09-02T06:39:10.527 回答
1

这可能会帮助任何在 UIPopovePresentationController 上遇到相同问题的人。根据我的经验,艾伦的回答也可以解决同样的问题。

我刚刚遇到了关于 Viewcontroller 委托的问题,该委托显示为 UIPopoverPresentingController 没有向其根视图发送调用。而艾伦的回答可以解决这个问题。

我的示例代码:

-(IBAction)choosePic:(id)sender
{
    UIButton *picButton = (UIButton *)sender;

    // Set PopoverPresentation view controller

    PicViewController *picVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PicViewController"];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:picVC];

    picVC.preferredContentSize = CGSizeMake(500., 500.);


    // Set this function as described in Allan's answer

    picVC.dismissPopover = ^(UIViewController *controller, UIImage *image) 
    {

       _myImage = image;

       .....

       .....

       .....

    };

    navController.modalPresentationStyle = UIModalPresentationPopover;

    _picPopover = navController.popoverPresentationController;

    _picPopover.delegate = self;

    _picPopover.sourceView = self.view;

    _picPopover.sourceRect = [picButton frame];

    navController.modalPresentationStyle = UIModalPresentationPopover;

    navController.navigationBarHidden = YES;

    [self presentViewController:navController animated:YES completion:nil];

}
于 2015-02-02T07:17:39.480 回答
1

你应该在你的 MediaPreviewViewController 中制定一个协议。然后当图像被删除时,发送一个委托方法以便父视图控制器可以处理它。

您也不应该让视图控制器自行关闭(尽管有可能,但建议创建模态的视图也负责删除模态......)

所以你应该得到这样的东西:

在 MediaPreviewViewController.h 中:

@protocol MediaPreviewViewControllerDelegate <NSObject>

-(void)didRemovedImage;

@end

@interface MediaPreviewViewController : NSObject {
    id< MediaPreviewViewControllerDelegate > delegate;
}

@property (nonatomic, assign) id < MediaPreviewViewControllerDelegate> delegate;

在 MediaPreviewViewController.m 中:

@synthesize delegate = _delegate;

然后在您的 MediaPreviewViewController 中删除图像的方法中,您只需调用:

[_delegate didRemoveImage];

在您的父视图控制器中,您需要像使用委托一样实现此协议。然后您还可以在此委托方法中从父视图中删除视图

于 2014-09-02T06:44:15.923 回答