11

Trong ứng dụng iOS của tôi, người dùng có thể chọn một hình ảnh từ danh sách, khi họ được trình bày với một phương thức có chứa hình ảnh và các tùy chọn để xóa hình ảnh. Nếu người dùng chọn xóa hình ảnh, cô ấy được trả về chế độ xem ban đầuController chứa danh sách hình ảnh. Tôi cần phải làm mới ViewController ban đầu để xem xét hình ảnh đã xóa.Làm mới Parent ViewController sau khi loại bỏ ModalViewController

Tôi đã thử sử dụng NSNotificationCenter để phát khi hình ảnh bị xóa sang Bộ điều khiển chế độ xem gốc. Tuy nhiên, có vẻ như chương trình phát sóng chưa bao giờ được nhận.

Có một số cách khác để

  1. gửi dữ liệu trở lại cho phụ huynh ViewController sau khi phương thức được bác bỏ, và
  2. phát hiện khi các phương thức đó bị cách chức mẹ ViewController?

(Tôi cố gắng làm theo tấm gương nêu here, nhưng nó dường như không làm việc)

Dưới đây là mã của tôi:

EditStepViewController (gốc View Controller):

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 (Chế độ xem thứ hai):

... 
[self deleteImage]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"MediaPreviewDismissed" object:nil userInfo:nil]; 
[self dismissViewControllerAnimated:YES completion:^(){ 
    NSLog(@"dismissed controller"); 
    }]; 

Sau đó, trở lại trong EditStepViewController:

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

Cảm ơn trước sự giúp đỡ của bạn!

+0

bạn có thể chỉ cho tôi nơi mà bạn đã thêm các quan sát viên trong editStepViewController bạn –

Trả lời

19

Trong trường hợp của tôi, tôi thường sử dụng khối ở đây.

Ví dụ, bạn có ParentViewController.h

@interface ParentViewController : UIViewController 
@end 

Thực hiện 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 

Và, 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

nhờ, điều này làm việc cho tôi! – scientiffic

+0

cảm ơn bạn. tiếng Anh> Tiếng Việt. – Saraz

1

Bạn nên thực hiện một giao thức trong MediaPreviewViewController của bạn. Sau đó, khi hình ảnh bị xóa, hãy gửi một phương thức ủy nhiệm để trình điều khiển xem của cha mẹ có thể xử lý điều đó.

Bạn cũng không bao giờ nên để chế độ xem bỏ qua chính nó (mặc dù điều đó là có thể, nhưng khuyến cáo rằng chế độ xem đã tạo phương thức cũng chịu trách nhiệm xóa phương thức ...)

Vì vậy, bạn sẽ nhận được một cái gì đó như thế này:

Trong MediaPreviewViewController.h:

@protocol MediaPreviewViewControllerDelegate <NSObject> 

-(void)didRemovedImage; 

@end 

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

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

Trong MediaPreviewViewController.m:

@synthesize delegate = _delegate; 

Sau đó, trong phương pháp của bạn trong MediaPreviewViewController nơi bạn loại bỏ các hình ảnh, bạn chỉ cần gọi:

[_delegate didRemoveImage]; 

Trong viewController cha mẹ của bạn, bạn cần phải thực hiện giao thức này như bạn đã quen với các đại biểu .. Sau đó, bạn cũng có thể loại bỏ các quan điểm từ cha mẹ trong phương pháp đại biểu này

1

này có thể giúp bất cứ ai có cùng một vấn đề trên UIPopovePresentationController. Từ kinh nghiệm của tôi, câu trả lời của Allan cũng có thể giải quyết cùng một vấn đề.

Tôi vừa mới có vấn đề về đại biểu ViewController rằng trình bày như UIPopoverPresentingController đã không gửi cuộc gọi đến xem gốc của nó. Và câu trả lời của Allan có thể giải quyết sự phù hợp này.

mẫu mã của tôi:

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

} 
Các vấn đề liên quan