在Swift中的视图控制器之间传递自定义类数据 [英] Pass custom class data between view controllers in Swift

查看:259
本文介绍了在Swift中的视图控制器之间传递自定义类数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器,一个包含一个tableview,另一个包含显示以下选择特定行的页面。



在我的viewController i我的类变量实例化

  var myTableViewItem = Item()

我从数据数组中获得了选定的行信息:



...



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()))//语句正确打印项目参数。
newController.itsItem = myTableViewItem as Item
}
}

并在目标视图控制器(newController)中具有可选类引用

的参数

  var thisItem:Item! 

然而,每当我试图访问后续视图控制器中的任何类数据时,它只保存默认初始化值。而不是从发送对象传递的那些



发送对象是绝对完全填充非默认值(用控制台打印语句验证)。我不知道为什么会发生这种情况,当我试图分配数据。



Ive在线搜索,看了两天的其他职位,现在无法使用! / p>

解决方案

感谢您的意见。我已经将问题缩小到类初始化问题。



我修改代码以在第二个视图控制器中包含一个可选变量:

  var:tester:string! 

并在第一个VC中添加了一行:

  var test = itemViewItem.itemName 
newController.tester = test

当第二个视图控制器加载时,我能够从viewDidLoad()打印出控制台的名称。因此问题是当我的自定义类在新的VC中分配一个值。



奇怪的是,如果我包含相同的对象初始化器,我在第一个控制器中的第一个:

  var itemTest:Item! 

itemTest = itemViewItem

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

这两个是等价的。然而,在第二个视图控制器中的值是第一次nil。然而,如果我按后退按钮,然后返回到视图控制器的第二/第三/第四次..它将工作正如预期从任何行(项)启动segue。领导我相信它与传递信息和类实现有关。


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!

解决方案

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.

这篇关于在Swift中的视图控制器之间传递自定义类数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆