11

在我的iOS應用程序中,用戶可以從列表中選擇一個圖像,並在其上顯示包含圖像的模式以及刪除圖像的選項。如果用戶選擇刪除圖像,她將返回到包含圖像列表的原始viewController。然後我需要刷新原始ViewController以考慮刪除的圖像。解除ModalViewController後刷新父視圖控制器

我試過使用NSNotificationCenter廣播時,圖像被刪除到父視圖控制器。但是,似乎廣播從未收到過。

有一些其他的方式來

  1. 將數據發送回模態後父視圖控制器被駁回,並
  2. 當模式是從父視圖控制器駁回檢測?

(我試過下面列出here的例子,但它似乎沒有工作)

下面是我的代碼:

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 
} 

預先感謝您的幫助!

+0

你能告訴我你在哪裏 – 2014-09-02 07:26:29

回答

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 

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 
+0

感謝添加觀察者在editStepViewController,這爲我工作! – scientiffic 2014-09-02 14:41:41

+0

謝謝。像魅力一樣工作。 – Saraz 2016-04-16 15:48:39

1

你應該做一個協議,在您的MediaPreviewViewController。然後,當圖像被刪除時,發送委託方法,以便父視圖控制器可以處理該方法。

你也不應該讓viewcontroller自行消除(雖然這是可能的,但建議創建該模式的視圖也負責刪除模態...)

所以,你應該得到的東西是這樣的:

在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]; 

在父視圖控制器,你需要實現這個協議就像你是用來與代表..然後,您還可以刪除從父視圖在此委託方法

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]; 

} 
相關問題