JamesG
JamesG

Reputation: 1601

Swift: Access Parent ViewController Segue from UIScrollView

What I have so far

I have a view controller ViewController1 which has 2 segues, one that goes to ViewController2 and one that comes back.

The segue from vc2 - vc1 is called "ViewC2toViewC1Segue"

On ViewController2 I have a UIScrollView that loads in two new viewcontrollers and allows me to scroll left or right to view them.

All these work fine, the data shows ok and everything displays nicely. However, On one of these subviews I want to be able to display an option to go back to ViewController1.

in my naivety I tried just using:

performSegueWithIdentifier("ViewC2toViewC1Segue", sender: self)

I hope this image helps explain:

enter image description here

The Problem

These two viewcontrollers that are loaded in the UIScrollView or not on the main storyboard so I can not CTRL and DRAG.

Question

How do I access the segue (ViewC2toViewC1Segue) of the view controller (ViewController2) that is holding the UIScrollView from one of UIScrollViews Child view containers.

I hope that makes sense.

Upvotes: 0

Views: 1436

Answers (2)

pdesantis
pdesantis

Reputation: 879

The best way to do this is with a delegate protocol. Your parent view controller would be the delegate of the child. When the button is pushed on the child, it messages its delegate (the parent, who has the scroll view), and the parent handles scrolling to the other view controller.


In your ChildViewController file, you want to do 3 things:

  1. Define the delegate protocol. This is a set of functions that the delegate object needs to implement.
  2. Add a delegate property to the ChildVC class. This allows the ChildVC to call functions on the delegate.
  3. Call the delegate function when the button is pressed

The protocol declaration would look something like this

protocol ChildViewControllerDelegate: class { func childViewControllerDidSelectBack(childViewController: ChildViewController) }

The delegate variable declaration would look like:

class ChildViewController: UIViewController { weak var delegate: ChildViewControllerDelegate? }

To call the delegate function, in your button handler code simply write:

delegate.childViewControllerDidSelectBack(self)


In your ParentViewController file, you want to do 3 things:

  1. Set yourself as the delegate for the ChildVC
  2. Declare that you conform to the ChildVCDelegate protocol
  3. Implement the delegate protocol methods

To set yourself as the delegate, whenever you instantiate the child VC, do something like:

childVC.delegate = self

To declare that you conform to the protocol, make your class definition look like:

class ParentViewController: UIViewController, ChildViewControllerDelegate

Lastly, you need to implement the protocol function

func childViewControllerDidSelectBack(childViewController: ChildViewController){ // code to scroll the scrollview } }


Hopefully this helps!

Upvotes: 1

rushisangani
rushisangani

Reputation: 3385

You can use notification approach here.

  • On button click of one of your ViewController (in scrollview) post notification.
  • Add observer in ViewController2's viewDidLoad method.
  • Go back to ViewController1 in selector method.

===================================================

//ViewController2

- (void)viewDidLoad
{
 [super viewDidLoad];

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(navigateToViewController1) name:@"navigateToViewController1" object:nil];

// Do any additional setup after loading the view from its nib.
}

-(void)navigateToViewController1 {
   //[self.navigationController popViewControllerAnimated:YES];
   OR
   // perform segue
}



//In button click event of your viewcontroller inside scrollview.

[[NSNotificationCenter defaultCenter] postNotificationName:@"navigateToViewController1" object:nil];

Upvotes: 0

Related Questions