首页 文章

如何将数组传递给swift中的另一个视图控制器?

提问于
浏览
1

我已经四处寻找一个很好的例子,说明如何传递一个在1视图控制器中创建和填充的数组,并通过模态segue传递给另一个 .

这是我的故事板

see here

用户单击历史记录按钮,然后弹出模式表视图控制器 . 然后使用发送或从原始视图传递的数组的内容填充表视图 .

当我在目标视图控制器中打印数组的内容时,它是空的 . 所以我认为它的初始化方式存在问题 .

到目前为止,这是我的代码

这是第一个UIView控制器

//UIPickerView BarButtonItem done button actions here

func donePicker() {

    txtHeight.resignFirstResponder()

    var feet = Int(item1.componentsSeparatedByString(" ")[0])!
    let inches = Int(item2.componentsSeparatedByString(" ")[0])!

    feet *= 12

    let totalInches = feet + inches

    totalHeightValues.append(totalInches)

    print(totalHeightValues)

    txtHeight.text = ""

    let alertController = UIAlertController(title: "\(feet / 12) ft" + " " + "\(inches) in", message: "Entry Sucessfully Added", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))

    self.presentViewController(alertController, animated: true, completion: nil)
}


//UIPickerView BarButtonItem cancel button actions here

func cancelPicker() {

    txtHeight.text = ""
    txtHeight.resignFirstResponder()

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "showHistory") {

        let dVC = (segue.destinationViewController as! HistoryTableViewController)

        dVC.totalHeightValuesDest = totalHeightValues
    }

}

这是Destination或2nd tableviewcontroller

import UIKit

class HistoryTableViewController: UITableViewController {

@IBAction func btnExit(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}


var totalHeightValuesDest: [Int] = []

override func viewDidLoad() {
    super.viewDidLoad()



    print(totalHeightValuesDest)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return totalHeightValuesDest.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = "\(totalHeightValuesDest[indexPath.row])"

    return cell
}

2 回答

  • -1
    • 在目标(HistoryTableViewController)中创建一个数组属性 .

    • prepareForSeque 的第一个视图控制器中,将属性分配给您当前拥有的数组 .

    • 继续使用segue

    在步骤#2中,在 dVC.totalHeightValuesDest = totalHeightValues 之后,添加以下内容: dVC.myNewArrayProperty = myCurrentArrayOfHistoryObjects . 显然,您将使用您在HistoryViewController中创建的属性名称和当前myCurrentArrayOfHistoryObjects替换您拥有的本地数组变量的名称 .

    确保正确地转换destinationViewController(首先是UINavigationController,然后调用topViewController来访问HistoryViewController) .

  • 2

    更改

    dVC.totalHeightValuesDest = totalHeightValues
    

    dVC.totalHeightValuesDest = self.totalHeightValues
    

相关问题