Reputation: 1601
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:
These two viewcontrollers that are loaded in the UIScrollView or not on the main storyboard so I can not CTRL and DRAG.
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
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:
delegate
property to the ChildVC class. This allows the ChildVC to call functions on the delegate.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:
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
Reputation: 3385
You can use notification approach here.
===================================================
//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