简体   繁体   中英

Removing presenting view from Modal View

I have a view hierarchy like this: MainView -> SubView (PresentingView) -> ModalViewc (all subclasses of UIViewController)

By clicking a button in the ModalView I want to remove both the ModalView and the PresentingView so I can return to the MainView which will then present ModalViewX:

MainView -> ModalViewX (all subclasses of UIViewController)

[[self presentingViewController] removeFromParentViewController];

The above code simply dismisses the ModalView but the PresentingView remains.

My final goal is to have the ModalView button perform some concluding logic before returning to the MainView. Clicking the button would basically do the following actions:

  1. Concluding logic
  2. Remove self(ModalView) and PresentingView (bonuspoints if I can animate the removal)
  3. Tell MainView to present ModalViewX (maybe I will have to call step 3 in the ModalView -> viewDidUnload method somehow)

Suggestions for how to do this?

First, embed your whole project in a UINavigationController with the MainViewController as the rootview. To get from the MainViewController to the SubViewController do this:

- (IBAction) theButtonClicked : (id) sender {
    SubViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YourIdentifier"];
    [self.navigationController pushViewController:vc animated:YES];
}

This will get you to the next UIViewController. If you want to pass information, just set vc setVariable: .

To get back to the rootview, simply call [self.navigationController popToRootViewControllerAnimated:YES]

The other way is to use segues, where you CTRL+drag from the button to the next UIViewController. The logic to be done should then be set in the method - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender , where the destionationcontroller is segue.destinationViewController .

That way, you can also add your custom animation by setting the seguestyle to custom, and subclassing UISegue .

For step 1 (ie, 'concluding logic'), use a target-action design. Specifically, this means that you'll add a method onto an object that will be invoked when a particular event occurs. For example, this can be done w/ a control-drag from a storyboard and into your view controller. The view controller you select is the target and the method you create is the action. More details are here: https://developer.apple.com/library/mac/#documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html .

To control the flow of view controllers, you have a couple different options.

For modal view controllers, it's a best practice to delegate the removal to the presenting view controller. This means that the modal view controller has a property called delegate that will receive a message when the modal view controller is ready to be removed (ie, the modal view controller does not remove itself). The delegate, which should conform to a protocol that you create, could handle any relevant concluding logic and call dismissViewControllerAnimated, which would remove the presented (modal) view controller.

Alternatively, you could use segues w/in a storyboard to manage the flow, as mentioned by Martol.

For more details about these approaches to managing view controller data and flow, check out: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ManagingDataFlowBetweenViewControllers/ManagingDataFlowBetweenViewControllers.html

Hope that helps.

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