Dove Devic Dove Devic - 5 months ago 21
iOS Question

Sending data through Unwind Segues

I have a custom unwind segue issue. Let me explain my controller setup:

I have a main screen which is a navigation controller with a cell view inside. Within this view, there is a button to add data, and in that view is 4 buttons for additional "types" of data to add. Once a user selects one of those four options, they are presented with a view controller specific to their choice that lets them enter data with a done button. The three screens respectively I will from now on refer to them as Main View, Options View, then Data View(all 4 options follow this convention).

After some tutorials I made a custom unwind segue in Main View's controller class, as follows:

@IBAction func unwindToTables(segue: UIStoryboardSegue) {
//function proto for adding data here
//populateTableWithData(data :ItemData)
}


In the storyboard editor, anytime I
control
+
click
from any View Controller to it's exit, it lists my unwind segue properly and this works fine.

I would now like to send data through the unwind. In Data View, I have a Done button that constructs and initializes a class of data, called ItemData to which I would like to pass through the segue.

Most of the questions I found on SO or other sites with this deals with normal segues, and when I try to implement them anyways the application causes a runtime error / does not compile.

Has anyone come into the issue before, and have a solution for said issue? Any help or points in the right direction would be much appreciated!

Edit:

I edited my unwind function as follows:

@IBAction func unwindToTables(segue: UIStoryboardSegue) {
let dataC = segue.sourceViewController as? DataVC
let data : ItemData = dataC!.SubmittedData! //unwrapping error

print(data.Name)


//save data

}


and on top of every New{1-4}ViewController I have the following:

import UIKit

class New1ViewController: DataVC,


and I made a new superclass DataVC:

import Foundation

class DataVC: UITableViewController {
var SubmittedData: ItemData?
}


but now I am getting an unwrapping issue... must I initialize SubmittedData prior?

Edit 2:

Thought I might add the Done button press I am doing...

@IBAction func DonePressed(sender: AnyObject) {

let n = ...
...
let d = ...

super.SubmittedData = ItemData(FieldType: "1", CName: n!, CPhone: p!, CAddress: a!, ShouldBe: s!, Repeats: r!, Starts: b, RemindMe: m!, AdditionalNotes: d!)

}

Answer

The UIStoryboardSegue that is passed to your unwindToTables: function has a property sourceViewControllee this will be the instance of your Data view controller.

You need all of your view controllers to inherit from a common superclass, so that you can add the property you need:

class DataVC: UITableViewController {
    var submittedData: ItemData?
}

Then you would have something like:

class New1ViewController: DataVC,... {
...
}

Then in your unwind method you can retrieve the submittedData property

@IBAction func unwindToTables(segue: UIStoryboardSegue) {
    if let dataVC = segue.sourceViewController as? DataVC {
      if let info = dataVC.submittedData {
         ...
      }
    }
 }
Comments