简体   繁体   中英

React on Modal ViewController being dismissed

I have three ViewControllers and one navigation controller. The Navigation Stack is:
NavigationController-push->VC1-push->VC2. VC1 can modally present VC3 in code. Its not connected via segues.
VC1 - "Your current projects"
VC2 - "Details of your project"
VC3 - "Create new project"

When user desires to create a new project, I put a VC3 using:

- (IBAction)newProjectButton:(id)sender {
    NewProjectViewController *newProject = [[NewProjectViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newProject];
    [self presentViewController:navController animated:YES completion:nil];
}

I user presses "Cancel" button, I use this code:

- (IBAction)cancelButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

When user presses "Done" button I want VC1 to make segue to VC2 and show new project's properties. I would like this segue to be invisible for user, so, he only sees this chain of events:
Presses the button "add" -> modal VC appears -> presses "Done" -> Modal VC disappears and VC3 is already shown.

What I am asking is how to tell VC1 that user pressed button "Done"? Is delegation possible here? How to implement it? Thank you.

You should create a delegate protocol which allows the modal view controller to send notifications to its creator.

@protocol ModalViewControllerDelegate
@optional
- (void)modalViewControllerDidCancel:(ModalViewController *)vc;
- (BOOL)modalViewControllerShouldSave:(ModalViewController *)vc;
@end

Then, in ModalViewController you define a new property. The weak is important, because you don't want to have any retain cycles.

@property (nonatomic, weak) id <ModalViewControllerDelegate> delegate;

Before dismissing or saving, just check if the delegate has implemented the methods (via -respondsToSelector: ) and send the appropriate callbacks. Don't forget setting the delegate property when creating your modal view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM