Reputation: 816
I am working on a app where i need to pass data throw a Navigation controller and back.
I got this to work using this tutorial: link
In my app i have from my first view controller(in tutorial uiviewcontroller) beside the push segue to secondViewcontroller(in tutorial fooTwoVC) also own to another view controller.
So is my question can i use the same protocol for that VC or do i need to make another protocol?
import UIKit
class ViewController: UIViewController,FooTwoViewControllerDelegate {
@IBOutlet var colorLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func myVCDidFinish(controller: FooTwoViewController, text: String) {
colorLabel.text = "The Color is " + text
controller.navigationController?.popViewControllerAnimated(true)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "mySegue"{
let vc = segue.destinationViewController as! FooTwoViewController
vc.colorString = colorLabel.text!
vc.delegate = self
}
}
}
import UIKit
protocol FooTwoViewControllerDelegate{
func myVCDidFinish(controller:FooTwoViewController,text:String)
}
class FooTwoViewController: UIViewController {
var delegate:FooTwoViewControllerDelegate? = nil
var colorString:String = ""
@IBOutlet var colorLabel : UILabel!
@IBAction func saveColor(sender : UIBarButtonItem) {
if (delegate != nil) {
delegate!.myVCDidFinish(self, text: colorLabel!.text!)
}
}
@IBAction func colorSelectionButton(sender: UIButton) {
colorLabel.text = sender.titleLabel!.text!
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
colorLabel.text = colorString
}
}
Upvotes: 1
Views: 480
Reputation: 816
After some time i finally figured it out and it wasn't that hard after all.
I just needed to change the protocol to:
protocol FooTwoViewControllerDelegate{
func myVCDidFinish(controller:UIViewController,text:String)
}
So i can be used by other view controllers. And further i just had the same code in the third view controller.
I you have any question feel free to ask.
Upvotes: 1