请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何快速将数组传递给另一个 View Controller ?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 16:57:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我环顾四周寻找一个很好的例子,说明如何传递在 1 个 View Controller 中创建和填充的数组,并通过模态 segue 将其传递给另一个。

这是我的 Storyboard

see here

用户单击历史按钮,它会弹出打开模态 TableView Controller 。然后用从原始 View 发送或传递的数组的内容填充表格 View 。

当我在目标 View Controller 中打印数组的内容时,它是空的。所以我认为它的初始化方式存在问题。

这是我目前的代码

这是第一个 UIView Controller

//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
}



Best Answer-推荐答案


  1. 在目的地(HistoryTableViewController)中创建一个数组属性。
  2. prepareForSeque 的第一个 View Controller 中,将属性分配给您当前拥有的数组。
  3. 继续转场

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

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

关于ios - 如何快速将数组传递给另一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633599/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap