How to pass data to another controller on dismiss ViewController?
I want to pass my data from one ViewController
to another VC on VC dismiss. Is it possible?
I've tried the next method and no success:
On button click:
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
controller.segueArray = [values]
}
when EditViewController
appears again, my segueArray
is nil
there.
How can I pass my data from my ViewController
to the EditViewController
on dismiss?
ios swift uiviewcontroller storyboard uistoryboardsegue
add a comment |
I want to pass my data from one ViewController
to another VC on VC dismiss. Is it possible?
I've tried the next method and no success:
On button click:
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
controller.segueArray = [values]
}
when EditViewController
appears again, my segueArray
is nil
there.
How can I pass my data from my ViewController
to the EditViewController
on dismiss?
ios swift uiviewcontroller storyboard uistoryboardsegue
add a comment |
I want to pass my data from one ViewController
to another VC on VC dismiss. Is it possible?
I've tried the next method and no success:
On button click:
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
controller.segueArray = [values]
}
when EditViewController
appears again, my segueArray
is nil
there.
How can I pass my data from my ViewController
to the EditViewController
on dismiss?
ios swift uiviewcontroller storyboard uistoryboardsegue
I want to pass my data from one ViewController
to another VC on VC dismiss. Is it possible?
I've tried the next method and no success:
On button click:
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
controller.segueArray = [values]
}
when EditViewController
appears again, my segueArray
is nil
there.
How can I pass my data from my ViewController
to the EditViewController
on dismiss?
ios swift uiviewcontroller storyboard uistoryboardsegue
ios swift uiviewcontroller storyboard uistoryboardsegue
edited Apr 30 '17 at 15:04
Jack
5,53232955
5,53232955
asked Apr 30 '17 at 13:00
John Doe
4813
4813
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:
Protocol for passing data:
protocol isAbleToReceiveData {
func pass(data: String) //data: string is an example parameter
}
Viewcontroller A:
class viewControllerA: UIViewController, isAbleToReceiveData {
func pass(data: String) { //conforms to protocol
// implement your own implementation
}
prepare(for: Segue) {
/** code for passing data **/
let vc2 = ViewCOntrollerB() /
vc2.delegate = self //sets the delegate in the new viewcontroller
//before displaying
present(vc2)
}
}
Dismissing viewcontroller:
class viewControllerB: UIViewController {
var delegate: isAbleToReceiveData
viewWillDisappear {
delegate.pass(data: "someData") //call the func in the previous vc
}
}
1
var delegate: isAbleToPassData
this code throwing error . it should bevar delegate: isAbleToPassData!
– Nazmul Hasan
Apr 30 '17 at 15:08
i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
– the_legend_27
May 1 '17 at 11:23
and call the func using delegate?.pass(data: "SomeData")
– the_legend_27
May 1 '17 at 11:24
where does the protocol get written out in ViewController A or ViewController B?
– Famic Tech
Dec 19 '18 at 4:01
add a comment |
In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
That you can achieve by iterating through your navigation stack's viewcontrollers like:
viewController.navigationController?.viewControllers.forEach({ (vc) in
if let editVC = vc as? EditViewController {
editVC.segueArray = ....
}
})
But I would recommend to use the delegate pattern, like:
protocol EditViewControllerDelegate: class {
func setSegueArray(segues: [UIStoryboardSegue])
}
In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:
class ViewController: UIViewController {
weak var delegate: EditViewControllerDelegate?
....
}
Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:
...
if let vc = presentingViewController as? ViewController {
vc.delegate = self
}
And conform the EditViewController to the delegate protocol like:
extension EditViewController: EditViewControllerDelegate {
func setSegueArray(segues: [UIStoryboardSegue]) {
// Do the data setting here eg. self.segues = segues
}
}
add a comment |
To detect when the back button is pressed on a view controller, I just use:
override func didMove(toParentViewController parent: UIViewController?) {
guard parent == nil else { return } // Back button pressed
... // Pass on the info as shown in you example
} // didMoveToParentViewController
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43706662%2fhow-to-pass-data-to-another-controller-on-dismiss-viewcontroller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:
Protocol for passing data:
protocol isAbleToReceiveData {
func pass(data: String) //data: string is an example parameter
}
Viewcontroller A:
class viewControllerA: UIViewController, isAbleToReceiveData {
func pass(data: String) { //conforms to protocol
// implement your own implementation
}
prepare(for: Segue) {
/** code for passing data **/
let vc2 = ViewCOntrollerB() /
vc2.delegate = self //sets the delegate in the new viewcontroller
//before displaying
present(vc2)
}
}
Dismissing viewcontroller:
class viewControllerB: UIViewController {
var delegate: isAbleToReceiveData
viewWillDisappear {
delegate.pass(data: "someData") //call the func in the previous vc
}
}
1
var delegate: isAbleToPassData
this code throwing error . it should bevar delegate: isAbleToPassData!
– Nazmul Hasan
Apr 30 '17 at 15:08
i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
– the_legend_27
May 1 '17 at 11:23
and call the func using delegate?.pass(data: "SomeData")
– the_legend_27
May 1 '17 at 11:24
where does the protocol get written out in ViewController A or ViewController B?
– Famic Tech
Dec 19 '18 at 4:01
add a comment |
The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:
Protocol for passing data:
protocol isAbleToReceiveData {
func pass(data: String) //data: string is an example parameter
}
Viewcontroller A:
class viewControllerA: UIViewController, isAbleToReceiveData {
func pass(data: String) { //conforms to protocol
// implement your own implementation
}
prepare(for: Segue) {
/** code for passing data **/
let vc2 = ViewCOntrollerB() /
vc2.delegate = self //sets the delegate in the new viewcontroller
//before displaying
present(vc2)
}
}
Dismissing viewcontroller:
class viewControllerB: UIViewController {
var delegate: isAbleToReceiveData
viewWillDisappear {
delegate.pass(data: "someData") //call the func in the previous vc
}
}
1
var delegate: isAbleToPassData
this code throwing error . it should bevar delegate: isAbleToPassData!
– Nazmul Hasan
Apr 30 '17 at 15:08
i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
– the_legend_27
May 1 '17 at 11:23
and call the func using delegate?.pass(data: "SomeData")
– the_legend_27
May 1 '17 at 11:24
where does the protocol get written out in ViewController A or ViewController B?
– Famic Tech
Dec 19 '18 at 4:01
add a comment |
The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:
Protocol for passing data:
protocol isAbleToReceiveData {
func pass(data: String) //data: string is an example parameter
}
Viewcontroller A:
class viewControllerA: UIViewController, isAbleToReceiveData {
func pass(data: String) { //conforms to protocol
// implement your own implementation
}
prepare(for: Segue) {
/** code for passing data **/
let vc2 = ViewCOntrollerB() /
vc2.delegate = self //sets the delegate in the new viewcontroller
//before displaying
present(vc2)
}
}
Dismissing viewcontroller:
class viewControllerB: UIViewController {
var delegate: isAbleToReceiveData
viewWillDisappear {
delegate.pass(data: "someData") //call the func in the previous vc
}
}
The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:
Protocol for passing data:
protocol isAbleToReceiveData {
func pass(data: String) //data: string is an example parameter
}
Viewcontroller A:
class viewControllerA: UIViewController, isAbleToReceiveData {
func pass(data: String) { //conforms to protocol
// implement your own implementation
}
prepare(for: Segue) {
/** code for passing data **/
let vc2 = ViewCOntrollerB() /
vc2.delegate = self //sets the delegate in the new viewcontroller
//before displaying
present(vc2)
}
}
Dismissing viewcontroller:
class viewControllerB: UIViewController {
var delegate: isAbleToReceiveData
viewWillDisappear {
delegate.pass(data: "someData") //call the func in the previous vc
}
}
edited Nov 19 '18 at 15:55
Soropromo
14616
14616
answered Apr 30 '17 at 13:14
the_legend_27
33119
33119
1
var delegate: isAbleToPassData
this code throwing error . it should bevar delegate: isAbleToPassData!
– Nazmul Hasan
Apr 30 '17 at 15:08
i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
– the_legend_27
May 1 '17 at 11:23
and call the func using delegate?.pass(data: "SomeData")
– the_legend_27
May 1 '17 at 11:24
where does the protocol get written out in ViewController A or ViewController B?
– Famic Tech
Dec 19 '18 at 4:01
add a comment |
1
var delegate: isAbleToPassData
this code throwing error . it should bevar delegate: isAbleToPassData!
– Nazmul Hasan
Apr 30 '17 at 15:08
i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
– the_legend_27
May 1 '17 at 11:23
and call the func using delegate?.pass(data: "SomeData")
– the_legend_27
May 1 '17 at 11:24
where does the protocol get written out in ViewController A or ViewController B?
– Famic Tech
Dec 19 '18 at 4:01
1
1
var delegate: isAbleToPassData
this code throwing error . it should be var delegate: isAbleToPassData!
– Nazmul Hasan
Apr 30 '17 at 15:08
var delegate: isAbleToPassData
this code throwing error . it should be var delegate: isAbleToPassData!
– Nazmul Hasan
Apr 30 '17 at 15:08
i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
– the_legend_27
May 1 '17 at 11:23
i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
– the_legend_27
May 1 '17 at 11:23
and call the func using delegate?.pass(data: "SomeData")
– the_legend_27
May 1 '17 at 11:24
and call the func using delegate?.pass(data: "SomeData")
– the_legend_27
May 1 '17 at 11:24
where does the protocol get written out in ViewController A or ViewController B?
– Famic Tech
Dec 19 '18 at 4:01
where does the protocol get written out in ViewController A or ViewController B?
– Famic Tech
Dec 19 '18 at 4:01
add a comment |
In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
That you can achieve by iterating through your navigation stack's viewcontrollers like:
viewController.navigationController?.viewControllers.forEach({ (vc) in
if let editVC = vc as? EditViewController {
editVC.segueArray = ....
}
})
But I would recommend to use the delegate pattern, like:
protocol EditViewControllerDelegate: class {
func setSegueArray(segues: [UIStoryboardSegue])
}
In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:
class ViewController: UIViewController {
weak var delegate: EditViewControllerDelegate?
....
}
Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:
...
if let vc = presentingViewController as? ViewController {
vc.delegate = self
}
And conform the EditViewController to the delegate protocol like:
extension EditViewController: EditViewControllerDelegate {
func setSegueArray(segues: [UIStoryboardSegue]) {
// Do the data setting here eg. self.segues = segues
}
}
add a comment |
In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
That you can achieve by iterating through your navigation stack's viewcontrollers like:
viewController.navigationController?.viewControllers.forEach({ (vc) in
if let editVC = vc as? EditViewController {
editVC.segueArray = ....
}
})
But I would recommend to use the delegate pattern, like:
protocol EditViewControllerDelegate: class {
func setSegueArray(segues: [UIStoryboardSegue])
}
In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:
class ViewController: UIViewController {
weak var delegate: EditViewControllerDelegate?
....
}
Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:
...
if let vc = presentingViewController as? ViewController {
vc.delegate = self
}
And conform the EditViewController to the delegate protocol like:
extension EditViewController: EditViewControllerDelegate {
func setSegueArray(segues: [UIStoryboardSegue]) {
// Do the data setting here eg. self.segues = segues
}
}
add a comment |
In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
That you can achieve by iterating through your navigation stack's viewcontrollers like:
viewController.navigationController?.viewControllers.forEach({ (vc) in
if let editVC = vc as? EditViewController {
editVC.segueArray = ....
}
})
But I would recommend to use the delegate pattern, like:
protocol EditViewControllerDelegate: class {
func setSegueArray(segues: [UIStoryboardSegue])
}
In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:
class ViewController: UIViewController {
weak var delegate: EditViewControllerDelegate?
....
}
Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:
...
if let vc = presentingViewController as? ViewController {
vc.delegate = self
}
And conform the EditViewController to the delegate protocol like:
extension EditViewController: EditViewControllerDelegate {
func setSegueArray(segues: [UIStoryboardSegue]) {
// Do the data setting here eg. self.segues = segues
}
}
In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
That you can achieve by iterating through your navigation stack's viewcontrollers like:
viewController.navigationController?.viewControllers.forEach({ (vc) in
if let editVC = vc as? EditViewController {
editVC.segueArray = ....
}
})
But I would recommend to use the delegate pattern, like:
protocol EditViewControllerDelegate: class {
func setSegueArray(segues: [UIStoryboardSegue])
}
In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:
class ViewController: UIViewController {
weak var delegate: EditViewControllerDelegate?
....
}
Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:
...
if let vc = presentingViewController as? ViewController {
vc.delegate = self
}
And conform the EditViewController to the delegate protocol like:
extension EditViewController: EditViewControllerDelegate {
func setSegueArray(segues: [UIStoryboardSegue]) {
// Do the data setting here eg. self.segues = segues
}
}
answered Apr 30 '17 at 13:21
Bence Pattogato
1,2961021
1,2961021
add a comment |
add a comment |
To detect when the back button is pressed on a view controller, I just use:
override func didMove(toParentViewController parent: UIViewController?) {
guard parent == nil else { return } // Back button pressed
... // Pass on the info as shown in you example
} // didMoveToParentViewController
add a comment |
To detect when the back button is pressed on a view controller, I just use:
override func didMove(toParentViewController parent: UIViewController?) {
guard parent == nil else { return } // Back button pressed
... // Pass on the info as shown in you example
} // didMoveToParentViewController
add a comment |
To detect when the back button is pressed on a view controller, I just use:
override func didMove(toParentViewController parent: UIViewController?) {
guard parent == nil else { return } // Back button pressed
... // Pass on the info as shown in you example
} // didMoveToParentViewController
To detect when the back button is pressed on a view controller, I just use:
override func didMove(toParentViewController parent: UIViewController?) {
guard parent == nil else { return } // Back button pressed
... // Pass on the info as shown in you example
} // didMoveToParentViewController
answered Apr 30 '17 at 13:52
KerCodex
657
657
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43706662%2fhow-to-pass-data-to-another-controller-on-dismiss-viewcontroller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown