【问题标题】:how to pass an array to another view controller in swift?如何快速将数组传递给另一个视图控制器?
【发布时间】:2016-06-04 18:21:27
【问题描述】:

我环顾四周寻找一个很好的例子,说明如何传递在 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
    }

}

这里是目标或第二个 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
}

【问题讨论】:

  • a) 仔细检查您的 segue 标识符,并且 b) 该 segue 的目标 VC 是导航控制器,HistoryTableViewController 将是 segue.destinationViewControllertopViewController

标签: ios arrays xcode swift


【解决方案1】:
  1. 在目标(HistoryTableViewController)中创建一个数组属性。
  2. prepareForSeque 的第一个视图控制器中,将属性分配给您当前拥有的数组。
  3. 继续转

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

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

【讨论】:

  • 我按照您的指示操作,它看起来和以前完全一样。 dVC.totalHeightValuesDest = totalHeightValues
  • @ThomasM。抱歉,代码没有滚动,所以我看不到你的 prepareForSegue 定义的其余部分。您是否确认在prepareForSegue 方法中分配它们时的值仍然如您预期的那样?如果是这样,我假设 HistoryViewController 中的 print(totalHeightValuesDest) 显示这些值没有保存?
  • 另外,你在哪里声明totalHeightValues
  • 我在第一个 ViewController 中公开声明它,我不断收到这个新错误。它说 id “无法将 'UINavigationController' (0x10bb58860) 类型的值转换为 '.HistoryTableViewController' (0x10a401590)。”知道那是什么意思吗?
  • 好的,现在我开始工作了——我不得不对整个 topViewController 做更多的研究,最后我用这个覆盖函数 prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if ( segue.identifier == "showHeightHistory") { 让 navVC = segue.destinationViewController 作为! UINavigationController 让 tableVC = navVC.viewControllers.first 为! HistoryTableViewController tableVC.totalHeightValuesDest = totalHeightValues } }
【解决方案2】:

改变

dVC.totalHeightValuesDest = totalHeightValues

dVC.totalHeightValuesDest = self.totalHeightValues

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多