ios - 将变量 (Int) 传递给 View 控件 (Popover)

标签 ios swift swift2

我有一个 tableView有 7 行,每一行是一个不同的 Action 。当用户点击一行时, View Controller 将显示为弹出窗口,并显示一个文本字段,供用户添加注释。然后我想将此注释保存到 Parse,但我需要用它保存 passedChildID,因此它 (passedChildID) 需要从 tableviewcontroller 传递.我在我的应用程序的其他区域中使用了此功能 tableView到另一个tableView但由于某种原因,它无法与弹出窗口一起使用。

以下代码在SingleChildViewTableViewController中.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "childToNote" {

        let popoverViewController = segue.destinationViewController
        popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
        popoverViewController.popoverPresentationController!.delegate = self

        let noteActionViewController = segue.destinationViewController as! actionNoteViewController
        if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) {
            noteActionViewController.passedChildID = passedChildID
        }
    }
    if segue.identifier == "childToItemNeeded" {
        //other stuff
    }
}

它构建并运行良好,但是当我点击该特定行时,出现以下错误。

Could not cast value of type '<<app_name>>.SingleChildTableViewController' (0x10e1bef60) to 'UITableViewCell' (0x110bca128).

我试过移动 let noteActionViewController... let popoverViewController 上方的代码认为前者永远不会运行,但这并没有改变任何事情。

segue 的执行方式如下(以防有任何差异)。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if indexPath.row == 0 {
        performSegueWithIdentifier("childToNote", sender: self)
    }
}

我很困惑,因为这段代码在我的应用程序的其他地方都有效。

最佳答案

看起来你的问题出在这一行

  indexPathForCell(sender as! UITableViewCell)

在这种情况下,你告诉它发送者是自己,它是一个 UIViewController

  performSegueWithIdentifier("childToNote", sender: self)

然后您在 indexPathForCell 行中告知将其转换为 UITableViewCell 但其类型为 SingleChildTableViewController。所以将发件人更改为 UITableViewCell

关于ios - 将变量 (Int) 传递给 View 控件 (Popover),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34386614/

相关文章:

iOS:如何从数字中获取正确的月份名称?

更改导航 Controller 框架后的 iOS 6 ViewController 框架问题

ios - 引用由另一个函数创建的数组

ios - 从 CoreData 中解包 Date() 类型的选项?

Swift 将动画与完成合二为一

iphone - 显示随机词和用户名

iOS - 在 Swift 中获取集合异步属性的最佳实践

ios - 当我快速单击图像时将 UIButtons 移回

ios - swift 2 : UITableViewDataSource protocol extension

ios - 在应用程序完成加载之前决定初始 View Controller