![](/img/trans.png)
[英]modal view controller not calling presenting view controller's dismissModalViewControllerAnimated: method
[英]Updating a presenting view controller's property inside the modal view controller
我想知道如何從模態視圖控制器內部調用的方法更新當前視圖控制器的視圖控制器property
? 現在,我能想到的唯一方法是使用NSNotificationCenter
,雖然這適用於Dictionary項,但我仍無法弄清楚如何將其用於自定義對象。
例如,我正在呈現的視圖控制器HomewViewController
具有一個名為homeSelections
的Parse PFObject
,可以在模態呈現的ModalViewController
的屬性newSelections
(以及PFObject
)中對其進行更新。 用戶做出選擇后,我希望HomeViewController
的homeSelections
也具有從模態視圖控制器傳遞的最新數據。
感謝您的幫助-謝謝。
更新1 :這是我現在所做的(請注意,我正在使用一個簡化的示例來測試事情)
在ViewController
(這是父/呈現視圖控制器)
@interface ViewController ()
@property (strong, nonatomic) NSArray *totalRamen;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.totalRamen = @[@"ramen1", @"ramen2", @"ramen3", @"moot"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"self.totalRamen: %@", self.totalRamen);
NSLog(@"Done");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showModal"]){
ModalViewController *destinationVC = (ModalViewController *)segue.destinationViewController;
destinationVC.passedRamen = self.totalRamen;
}
}
- (IBAction)showModalAction:(UIButton *)sender
{
ModalViewController *destinaionViewController = [[ModalViewController alloc] init];
destinaionViewController.selectionCallback = ^(id selectedItem) {
self.totalRamen = (NSArray *)selectedItem;
NSLog(@"self.totalRAmen %@", self.totalRamen);
NSLog(@"done");
};
[self performSegueWithIdentifier:@"showModal" sender:self];
}
在ModalViewController
(這是顯示/模態視圖控制器)
@interface ModalViewController : UIViewController
@property (strong, nonatomic) NSArray *passedRamen;
- (IBAction)dismissModal:(UIButton *)sender;
typedef void(^CallbackBlock)(id value);
@property (nonatomic, copy) CallbackBlock selectionCallback;
@end
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"Passed ramen %@", self.passedRamen);
NSLog(@"Done");
self.passedRamen = @[@"moot is awesome"];
NSLog(@"new ramen: %@", self.passedRamen);
NSLog(@"%@", self.selectionCallback); //nil here
//call back
CallbackBlock selectionCallback = self.selectionCallback;
if (selectionCallback){
selectionCallback(self.passedRamen); //I want to send the newly updated self.passedRamen back
} else {
NSLog(@"No show"); //Means if isn't called
}
}
- (IBAction)dismissModal:(UIButton *)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
將回調塊傳遞到顯示的視圖控制器中。 這樣,所呈現的視圖控制器對呈現它的視圖控制器一無所知。 更加靈活,因為現在任何人都可以展示您的視圖控制器,他們只需將其傳遞一個塊即可!
PresentedViewController *vc = [[PresentedViewController alloc] init]; //or get your existing one
vc.selectionCallback = ^(id selectedItem) {
//update selected items here
};
//present vc here
typedef void(^CallbackBlock)(id value);
@property (nonatomic, copy) CallbackBlock selectionCallback;
- (void)somethingWasSelected:(id)selectedItem {
CallbackBlock selectionCallback = self.selectionCallback;
if (selectionCallback) selectionCallback(selectedItem);
}
當心保留周期 。 此塊由呈現的視圖控制器保留,因此在不先削弱該塊的情況下對塊中呈現的視圖控制器的引用將產生泄漏。 有關此的更多信息,請參見此處 。
用戶做出選擇后,我希望HomeViewController的homeSelections也具有從模態視圖控制器傳遞的最新數據。
最簡單的方法是完全避免ModalViewController
更新HomeViewController
。 扭轉通信-讓HomeViewController
查詢ModalViewController
並在ModalViewController
模態時更新自身。
HomeViewController
已經依賴ModalViewController
-它必須了解ModalViewController
才能顯示它。 因此,讓它也知道如何在適當的時間從ModalViewController
中讀取newSelections
屬性是沒有害處的。 另一方面, ModalViewController
無需了解其信息的ModalViewController
。 它不需要了解HomeViewController
即可完成其工作。 如果您避免告訴ModalViewController
有關HomeViewController
任何信息,則可以在需要時從其他視圖控制器輕松使用ModalViewController
。 更重要的是,如果HomeViewController
發生更改,則無需再更新ModalViewController
。
例如,您的HomeViewController
可能看起來(部分)如下所示:
- (void)showModalViewController
{
self.modalViewController = [[ModalViewController alloc] init]; // or otherwise get the modal controller
[self presentViewController:self.modalViewController
animated:YES
completion:];
}
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
self.homeSelections = self.modalViewController.newSelections;
[super dismissViewControllerAnimated:flag completion:completion];
self.modalViewController = nil;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.