Passing data from modal segue to parent - ios

I want to pass data (e.g. set var) from modal segue to parent, how can I do that?
I’m using that code to exit from modal segue:
#IBAction func doneClicked(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
I can’t use segue.destinationViewController here to pass data as i’m used to do on push segues.

Create protocol on Modal ViewController
protocol ModalViewControllerDelegate
{
func sendValue(var value : NSString)
}
Also declare in you Modal ViewController class
var delegate:ModalViewControllerDelegate!
Include this protocol ModalViewControllerDelegate in ParentViewController
When you are Moving form one viewController to another
modalVC.delegate=self;
self.presentViewController(modalVC, animated: true, completion: nil)
Here you get your value in ParentViewcontroller
func sendValue(value: NSString) {
}
Finally on ModalViewController
#IBAction func doneClicked(sender: AnyObject) {
delegate?.sendValue("value")
self.dismissViewControllerAnimated(true, completion: nil)
}

In the second viewController (the one showed by the segue) declare a variable like
var parentVC : UIViewController?
then when you call segue from parent
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "yourSegue" {
let secondController= segue.destinationViewController as UIViewController
secondController.parentVC = self
}
}
so you can use
#IBAction func doneClicked(sender: AnyObject) {
self.parentVC.yourVariable = 0
self.dismissViewControllerAnimated(true, completion: nil)
}

Related

Can't pass datas back from UIView Popover Dismiss

I have set a "Show as popover" segue between a UIView (A) and another UIView (B) (embed in a Navigation Controller) activated on a button's clic.
i am trying to pass datas back from (B) to (A) when i dismiss it (i want to keep the popover animation on both ways).
I have tried many methods i found mostly here, on Stackoverflow, but as of now i never successfully retrieved my data on (A).
I tried Delegates and protocols as well as other simpler methods. The last in date is the following one:
In (A), i just try to print the variable that should be storing the datas in ViewWillAppear :
class SearchBarsController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
var testValue:String = ""
override func viewWillAppear(_ animated: Bool) {
print(testValue) // print is empty
super.viewWillAppear(animated)
}
}
In (B), i dismiss the popover and try to send the datas back on a button clic :
class SearchFilterViewController: UIViewController {
#IBAction func DismissPopoverOnClic(_ sender: Any) {
if let navController = presentingViewController as? UINavigationController {
let presenter = navController.topViewController as! SearchBarsController
presenter.testValue = "Test"
print("success") //never called
}
self.dismiss(animated: true, completion: nil)
}
}
on (B) i'd like to set up some filter that i'd use on (A) to present search results in a tableview. But actually the testValue's value is always blank.
oky so you can do it using unwind segue here is sample project :
sample projecct
process :
Add this method to SearchBarsController below viewWillAppear
#IBAction func unWindFromFilterViewController(_ sender: UIStoryboardSegue) {
}
Than go to Storyboard and go to SearchFilterViewController and then cntrl + Drag from DismissPopoverOnClic to top of the exit button then select unWindFromFilterViewController .
Than this the SearchFilterViewController write this method for passing data
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? ViewController {
destVC.testValue = "Test"
}
}
You will get your desired data back . thanks
When passing back data to a viewController, the most efficient way to implement it using delegate
protocol SearchFilterViewControllerDelegate {
func setTextValue(string : String)
}
class SearchFilterViewController: UIViewController {
var delegate : SearchFilterViewControllerDelegate?
#IBAction func DismissPopoverOnClic(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
delegate?.setTextValue(string : "Test Value")
}
}
class SearchBarsController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
var filterViewController : SearchFilterViewController?
func popup() {
// your pop up code and init filterViewController
filterViewController.delegate = self **//without this line, the delegate will be nil, no nothing will happen.**
}
}
extension SearchBarsController : SearchFilterViewControllerDelegate {
func setTextValue(string : String) {
print(string)
}
}

How to go from child UIViewController to parent UIViewController and from there to another child UIViewController

I have a parent UIViewController(MainVC). From there I have 2 segue to 2 UIViewControllers: FirstVC (identifier: goFirstVC) and SecondVC (identifier: goSecondVC)
In FirstVC I have a button Save and when I click it I want to dismiss the FirstVC and to go on SecondVC.
Here is my code:
#IBAction func saveBtnTapped(_ sender: UIButton) {
//navigationController?.popViewController(animated: true)
let destinationController = self.storyboard?.instantiateViewController(withIdentifier: "goSecondVC") as! SecondVC
let presentingVC = self.presentingViewController
self.dismiss(animated: false, completion: { () -> Void in
presentingVC!.present(destinationController, animated: true, completion: nil)
})
}
Here is the design for my issue:
You can use setViewControllers to keep the parent only and the SecondVC
let destinationController = self.storyboard?.instantiateViewController(withIdentifier: "goSecondVC") as! SecondVC
self.navigationController?.setViewControllers([self.navigationController!.viewControllers.first!,destinationController], animated: true)
There are many methods but one of the generic one is to use delegates and protocols. Use the following code in your classes.
Add Following code in the first VC
protocol SecondVCDelegate : AnyObject {
func goToSecondVC()
}
class FirstVC: UIViewController {
var Delegate : SecondVCDelegate!
#objc func save() {
Delegate.goToSecondVC()
}
}
//In second view
Add following code in MinVC
class MainVC: UIViewController {
override func viewDidAppear() {
self.performSegue(withIdentifier: <yourSegueIdentifierToFirstVC>, sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == <yourSegueIdentifierToFirstVC> {
let cont = segue.destination as! FirstVC
cont.Delegate = self
}
}
}
extension MainVC : SecondVCDelegate {
func goToSecondVC() {
self.performSegue(withIdentifier: <yourSegueIdentifierToSecondVC>, sender: nil)
}
}
//This is MainVC
I think you should present second from first rather than dismissing first then presenting second.
when you will present second from first then if you wanto go back to main you can simply dismiss 2 veiwcontrollers at once without any hack.
try it:
We can control our presented controllers with navigation controller calling pushViewController and popViewController methods.
FirstVC.navigationController?.popViewController(animated: animated) // pops the top view controller
self.navigationController?.pushViewController(SecondVC, animated: true) // Pushes a view controller onto navigation's stack of controllers
In your case:
let destinationController = self.storyboard?.instantiateViewController(withIdentifier: "goSecondVC") as? SecondVC
self.navigationController?.popViewController(animated: animated)
self.navigationController?.pushViewController(destinationController, animated: true)

How to trigger some code when back from "Over Current Content" ViewController?

There are two ViewController in my app, ViewController and ViewController2
In ViewController, a button set Present Modally segue to "ViewController2"
And ViewController override viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("will appear")
}
In ViewController2, a button to go back
#IBAction func close(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
Now it still can trigger viewWillAppear then I go back to ViewController from ViewController2
If I change ViewController2's presentation from Full Screen to Over Current Context, viewWillAppear will not be triggered
How can I trigger some code when go back?
You can do it without giving up storyboard segues, but you nevertheless had to setup will/did Disappear handler in ViewCOntroller2:
class ViewController: UIViewController {
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ViewController2 {
(segue.destination as? ViewController2).onViewWillDisappear = {
//Your code
}
}
}
}
class ViewController2: UIViewController {
var onViewWillDisappear: (()->())?
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
onViewWillDisappear?()
}
...
}
There are several ways to handle this operation. Here is one, which I used to use.
// ViewController1
class ViewController1: UIViewController {
#IBAction func presentOverCurrentContext(button: Button) {
let vc2 = // instantiate ViewController2
vc2.modalPresentationStyle = .overFullScreen
vc2.presentingVC = self // use this variable 'presentingVC' to connect both view controllers
self.present(vc2, animated: true)
}
}
// ViewController2
class ViewController2: UIViewController {
var presentingVC: UIViewController? // use this variable to connect both view controllers
#IBAction func close(button: Button) {
// handle operation here
presentingVC?.viewWillAppear(true)
self.dismiss(animated: true, completion: {
// or here
// presentingVC?.viewWillAppear(true)
})
}
}
You can also use, your own method to reload view/viewcontroller, but viewWillAppear is common accessible method for all view controllers (as part of super class life cycle) hence you may not need to specify custom type of view controller for presentingVC
While the answers so far provided do work I think it's a good idea to show how to do it using a protocol and delegate as that's a clean implementation which then also allows for further functionality to be added with minimal effort.
So set up a protocol like this:
protocol SecondViewControllerProtocol: class {
func closed(controller: SecondViewController)
}
Setup the second view controller like this:
class SecondViewController {
public weak var delegate: SecondViewControllerProtocol?
#IBAction func close(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
self.delegate?.close(controller: self)
}
}
Setup the first view controller like this:
class FirstViewController: SecondViewControllerProtocol {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SecondViewControllerID",
let secondViewController = segue.destination as? SecondViewController {
secondViewController.delegate = self
}
}
func closed(controller: SecondViewController) {
// Any code you want to execute when the second view controller is dismissed
}
}
Implementing it like this does what the original request was and allows for extra methods to be put in the protocol so that the FirstViewController can respond to other actions in the SecondViewController.
Note:
You might want to move the delegate method call into the closure of the dismiss handler so that you know the method is not called until the SecondViewController is actually gone (in case you try to present another view which would fail). If that's the case you could do this:
#IBAction func close(_ sender: Any) {
self.dismiss(animated: true) {
self.delegate?.close(controller: self)
}
}
In fact you could have a will and did methods and call them like this:
#IBAction func close(_ sender: Any) {
self.delegate?.willClose(controller: self)
self.dismiss(animated: true) {
self.delegate?.didClose(controller: self)
}
}
Which would allow you to do something immediately while the second controller is animating away and then know when it has actually gone.
Best/Clean way to handle this scenario to use call back handler.
Example Code
typealias CloseActionHandler = ()-> Void
class TestController: UIViewController {
var closeActionHandler: CloseActionHandler?
func close(_ handler:#escaping CloseActionHandler) {
self.closeActionHandler = handler
}
#IBAction func closeButtonTapped(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
self.closeActionHandler?()
}
}
class ViewController: UIViewController {
func loadTestController(viewController: TestController) {
viewController.close {
//will be called when user will tap on close button
}
}
}

how pass data between view controller and TableViewController when using dismiss page that opened with performSegue in swift 4?

I used this code here to pass data from first view controller to the second view controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? secondViewController {
vc.showPageType = self.checkEdit
}
But the problem is that in the second view controller I have text field that when user fill that text field and push the button submit the secondViewController will be dismiss with this method
dismiss(animated: false, completion: nil)
and now I can't use perform segue method to pass textfield text to the first view controller how can I do that in swift4?
Add to your secondViewController source code file:
protocol SecondViewControllerDelegate {
func submitButtonPushedWithText(_ text: String)
}
Add to class secondViewController property:
var delegate: SecondViewControllerDelegate?
Then conform your first controller to SecondViewControllerDelegate and implement method submitButtonPushedWithText(:):
class FirstViewController: UIViewController, SecondViewControllerDelegate {
func submitButtonPushedWithText(_ text: String) {
// use text from textField of second controller
}
}
Also setup delegate property of second controller before presenting:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? secondViewController {
vc.showPageType = self.checkEdit
// setup delegate
vc.delegate = self
}
Now you can call method submitButtonPushedWithText(_ text: String) in your Second controller just before calling dismiss(animated: false, completion: nil):
func submitButtonPushed() {
delegate?.submitButtonPushedWithText(textField.text!)
dismiss(animated: false, completion: nil)
}

Segue not running after MPC viewcontroller is dismissed

I have an action tied to pop up the MPC view controller however once this action is complected and the browser is dismissed I simply am lead back to my old view controller
func browserViewControllerDidFinish(
browserViewController: MCBrowserViewController!) {
// Called when the browser view controller is dismissed (ie the Done
// button was tapped)
func seguetoJoin(segue: UIStoryboardSegue, sender: AnyObject?) {
var joinView: playMusicViewController = segue.destinationViewController as! playMusicViewController
weak var delegate: MCBrowserViewControllerDelegate!
}
self.dismissViewControllerAnimated(true, completion: nil)
}
You seem to be calling dismissViewController on the VC that is receiving the didFinish callback.
You should move the segue code to the ViewController that needs to perform the segue, rather than performing it on a ViewController that is being dismissed.
Also, these are unused
var joinView: playMusicViewController = segue.destinationViewController as! playMusicViewController
weak var delegate: MCBrowserViewControllerDelegate!
Update -
Following the comments below.
in mpcSubClass.m
var viewControllerToPeformSegue: UIViewController!
func browserViewControllerDidFinish(browserViewController: MCBrowserViewController!) {
// animation:false as your segue will have an animation
self.dismissViewControllerAnimated(false, completion: nil)
self.viewControllerToPerformSegue.handleBrowserDidFinish()
}
in viewControllerToPerformSegue.m
// when you initialise the MPC VC subclass
mpcVCSubclass.viewControllerToPerformSegue = self
func handleBrowserDidFinish()
{
self.performSegueWithIdentifier("mainViewSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if segue.identifier == "mainViewSegue"
{
let mainVC = segue.destination as! MainVC
// now pass any data to your main VC that it can load in viewWillAppear
}
}
in storyboard
link viewControllerToPerformSegue to the MainVC and name the segue mainViewSegue
Hope this helps.

Resources