Pass custom class data between view controllers in Swift

会有一股神秘感。 提交于 2019-12-11 02:54:01

问题


I have two view controllers one that contains a tableview and another that contains the page to show following selection of a particular row.

At the start of my viewController i have my Class variable instantiated

var myTableViewItem = Item()

I have got the selected row information from the data array:

...

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         myTableViewItem = pList[indexPath.row]
         println("\(itemViewItem.getItemName())")
        performSegueWithIdentifier("itemSelect", sender: myTableViewItem)
}



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "itemSelect" {
        let nController = segue.destinationViewController as UINavigationController
        let newController = nController.viewControllers[0] as ItemDetailViewController
        println("\(myTableViewItem.getItemValue())") //Statement correctly prints item parameter.
        newController.itsItem = myTableViewItem as Item
    }
}

and have a parameter in the destination view controller (newController) with an optional class reference

var thisItem: Item!

however whenever i attempt to access any class data in the subsequent view controller it only ever holds default initialiser values. Instead of those passed from the sending object

The sending object is definitely fully populated with non-default values (as verified with console print statements). i can't figure out why this is happening when i attempt to assign the data.

Ive searched online and looked at other posts for two days now to no avail!


回答1:


Changed answer based on comments below:

If I understand correctly,

make the property an optional

var  thisItem: Item?

Then in viewDidLoad method check if it is nil to set the default value, it will likely have a value here if you are setting it previously:

func viewDidLoad() {
if thisItem! == nil{
 thisItem = theDefaultValue
}

....

Old Answer: If you can't figure out where the value is being changed, I would add a setter/getter with a breakpoint as such

var _thisItem: Item?
var  thisItem: Item? {
  get {
    return _thisItem;
  }

  set {
    _thisItem = newValue;
    println("value was changed to \(newValue)");//add a breakpoint here and check trace.
  }
}



回答2:


Thanks for your input. I've narrowed the problem down to a class initialisation issue.

i modified the code to include an optional variable in the second view controller:

var: tester:string!

and included an additional line in the first VC:

var test = itemViewItem.itemName
newController.tester = test

when the second view controller loads i am able to print out the name to the console from viewDidLoad(). therefore the problem is that when my custom class is assigned a value in the new VC. It is not able to take the value of given to it for some reason.

oddly though if i include the same object initialiser that i had in the second controller within the first:

var itemTest: Item!

itemTest = itemViewItem

println(itemTest.itemName)
println(itemViewItem.itemName)

the two are equivalent. However the value that in the second view controller is 'nil' the first time around. however if i press the back button and then return to the view controller the second/third/forth.. times it will work as expected from then on with whatever row (item) initiates the segue. Leading me to believe its something to do with passing information and class implementation.



来源:https://stackoverflow.com/questions/29992318/pass-custom-class-data-between-view-controllers-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!