2015-03-26 14 views
7

Đầu tiên, tôi tạo một MainViewController. Sau đó, trong MainViewController, tôi làmViewDidAppear không được gọi khi chế độ xem phương thức bị loại bỏ

[self presentViewController:modalViewController animated:YES completion:nil]; 
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet; 

Khi tôi bỏ qua những modalViewController, On iPhone (trừ iPhone 6+), viewDidAppear của MainViewController được gọi. Trên iPad và iPhone 6+, viewDidAppear của MainViewController không được gọi.

Logic được gọi là hàm khi modalViewController bị loại bỏ. Làm thế nào tôi có thể biết khi modalViewController được loại bỏ.

+0

Hãy thử sử dụng một số 'modalPresentationStyle' khác. Nó có hoạt động không? – itsji10dra

+0

Đó là yêu cầu của UX. Tôi không thể thay đổi nó. – Gonghan

+0

Bạn có đang gọi: [super viewDidAppear: animated]; – itsji10dra

Trả lời

4

Bạn có thể sử dụng đại biểu để gọi hàm của mình trong MainViewController khi bạn loại bỏ bộ điều khiển chế độ xem phương thức. Ví dụ:

MainViewController.h:

@protocol YourDelegate <NSObject> 
- (void)someFunction; 
@end 

@interface MainViewController : UIViewController <YourDelegate> 

@end 

MainViewController.m:

// Where you present the modal view 
ModalViewController *view = [[ModalViewController alloc] init]; 
view.delegate = self; 
[self presentViewController:view animated:YES completion:nil]; 

ModalViewController.h:

@interface ModalViewController : UIViewController 
@property (nonatomic, weak) id<YourDelegate> delegate; 
@end 

ModalViewController.m

// Wherever you dismiss.. 
[self dismissViewControllerAnimated:YES completion:^{ 
    [self.delegate someFunction]; 
} 
+0

nếu dự án sử dụng bảng phân cảnh + phân đoạn thì sao? Tôi có nghĩa là tình hình khi segue được định nghĩa trong storyboard, không phải trong mã –

3

Cách mà Apple cung cấp bộ điều khiển chế độ xem thực hiện việc này là có một đại biểu trên bộ điều khiển xem được trình bày, được gọi khi bộ điều khiển xem đó yêu cầu đóng. Sau đó, người trình bày sẽ chịu trách nhiệm loại bỏ bộ điều khiển, và do đó cũng sẽ biết khi nào làm bất kỳ việc dọn dẹp nào liên quan (cả trước và sau hoạt ảnh).

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