Reputation: 25
I Used "performSegueWithIdentifier()" from 'A-VIEW' to jump to the 'B-VIEW'
Now, I want to return to A,B carry the parameter transfer to A
B:
protocol changeDelegate : NSObjectProtocol{
func returnCard(controller:UIViewController,labelText:NSString)
}
var delegate:changeDelegate?
@IBAction func rrrr(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion:{
println("start")
self.delegate?.returnCard("1234")
})
}
A:
class AViewController: UIViewController,changeDelegate{
....
func returnCard(labelText: NSString) {
println(labelText)
}
}
It is not output
Upvotes: 1
Views: 4772
Reputation: 5532
The key to this riddle is so obvious.
When you connect the button on the second screen (the one you are returning FROM) Ctrl drag it to the Exit and it will offer you a bunch of IBAction routines to go to. You need to choose the one you ALREADY prepared in the first Screen (the one you are returning TO).
Then it's as smooth as cream.
Upvotes: 0
Reputation: 2795
This is what I have used:
protocol ValueReturner {
var returnValueToCaller: ((Any) -> ())? { get set }
}
in A:
override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
if let secondViewController = segue.destinationController as? ValueReturner {
secondViewController.returnValueToCaller = someFunctionThatWillHandleYourReturnValue
}
}
func someFunctionThatWillHandleYourReturnValue(returnedValue: Any) {
// cast returnedValue to the returned value type and do what you want. For example:
if let string = returnedValue as? String {
println(string)
}
}
in B:
class SecondViewController: NSViewController, ValueReturner {
var returnValueToCaller: ((Any) -> ())?
@IBAction func rrrr(sender: AnyObject) {
returnValueToCaller?("1234")
dismissViewController(self)
}
}
This way SecondViewController doesn't have to know anything about FirstViewController and it can pass back any value. I haven't tried using dismissViewControllerAnimated so I don't know if that works.
Upvotes: 1
Reputation: 6612
You can use the Exit (i.e. unwind your segue) provided.
In your first view controller you need to add an IBAction function, with a UIStoryboardSegue as a parameter. Here we'll call it @IBAction func unwindFromSecondVC(segue: UIStoryboardSegue)
. Inside which you'll be able to access the view controller you're coming back from.
In your second view controller you can perform the action you have to do. In the view itself (storyboard), you just place a button in order to go back when you'll need. Link this button to the Exit of the view controller (orange square on top of it, see capture at bottom). This could also be done programmatically if you don't need a button.
ViewController.swift :
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Here, you pass your parameter(s) to the secondVC.
}
@IBAction func unwindFromSecondVC(segue: UIStoryboardSegue) {
// Here you can receive the parameter(s) from secondVC
let secondVC : SecondViewController = segue.sourceViewController as SecondViewController
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// You can do stuff here (but to go forward not backward in segue).
// But in your case, see firstVC.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Storyboard : Example from another Q/A : http://goo.gl/J35mC6
Upvotes: 3