HpTerm
HpTerm

Reputation: 8281

Dismissing/Delegate second view in a modal stack in storyboard

I have a TableView which describes a book with sections which represents the chapters and rows representing the verses.

A the top of this TableView I have a button in a navigation bar to allow "navigation".

The goal of this navigation button is to allow the user to easily jump to a given chapter/verse without scrolling manually (which can be very long).

When the button is pressed a tableview controller is called displaying all the available chapters of the book and when a chapter is selected another table view is called displaying a list of the available verses in the current chapter. Finally when the line is chosen the tablew view displaying the book should scroll to the given index/row.

So the idea : from the tableview representing the book I call the chapters view as modal and the verses as a push over the chapters view.

enter image description here

My problem is that I don't get the point of managing the delegate and dismissing from the 2nd modal view.

With 1 modal view I do things like that.

In the displayed VC (View Controller) I added the protocol and the delegate

@protocol ChapitresTableViewControllerDelegate <NSObject>
- (void)didDismissPresentedViewController;
@end

@interface ChapitresTableViewController : UITableViewController
@property (nonatomic, weak) id <ChapitresTableViewControllerDelegate> delegate;
@end

I have in the didSelectRow

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate didDismissPresentedViewController];
}

in the displaying VC I add the following line

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  ChapitresTableViewController *chapitresTableViewController = segue.destinationViewController;
  chapitresTableViewController.delegate = self;
}

and of course

-(void)didDismissPresentedViewController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

this would just work fine if I wanted to close after the first modal VC, but what I want is to have the second one being dismissed after I click in the second. Ok I can write the protocol and stuff in the second modal VC but how do I manage to have the delegate being send to the second VC.

Hope my question is clear enough it is not so easy to explain it.

Anyone understand me and can help me ?

NOTE : I know for now I don't pass any parameters back to the delegate, which I will do later to do the scroll. For now I just want to be able to close the second view, then I will add the required stuff to pass the parameters back to the delegate

Upvotes: 0

Views: 326

Answers (1)

Rob
Rob

Reputation: 437682

I'm sure you can do this, but rather than modal view controllers with a navigation bar, wouldn't it be easier to use a navigation controller? Then you can use popToViewController to go back as many levels as you want to a particular view controller. You can either pass the UIViewController* of the various controllers you might want to pop to, or do so programmatically: e.g. How to pop back to specify viewController from navigationController(viewControllers/stack)?

In this scenario previous views controllers are retained. The ones you pop off are released (just like the modal ones you dismiss are released), but the ones that you pushed from are retained (just like the ones you presented from in a modal world are retained).

If the book is large, though, you'll have to be sensitive to memory usage. Thus, you will probably want to handle didReceiveMemoryWarning to release the model data for the previous views in either your modal sequence or push sequence, in which case, on viewDidAppear, you'll want to see if your app had to release the memory in response to didReceiveMemoryWarning and reload it in that case. But that's the desired behavior, either way, gracefully release the pages if needed (and reload them when the particular view reappears), but keep it in memory if you can.

Finally, you might also want to contemplate using UIPageViewController. Given what you've described, I'd like consider UIPageViewController first, UINavigationController and push segues second, and the use of modal segues third.

Upvotes: 1

Related Questions