【问题标题】:Passing integer with Segue使用 Segue 传递整数
【发布时间】:2017-05-26 18:54:49
【问题描述】:

我在尝试向新的 viewController 发送数据时遇到问题。 这一行执行

destination1.path = path

因为我获得了新 ViewController 的路径,所以整数似乎有些问题。

我收到此错误消息:

2017-05-26 20:37:14.442509+0200 订阅[1224:301622] UIView: 0x105d294f0;帧 = (0 0; 414 736);自动调整大小 = W+H; layer = CALayer: 0x17403f360>> 的窗口不等于 Subscription.ViewController2: 0x105d3fcb0> 的视图窗口!

// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped cell number \(indexPath.row).")

    let index = indexPath.row
    performSegue(withIdentifier: "EditSegue1", sender: index)


}
@IBAction func backButton(_ sender: Any) {
     self.dismiss(animated: true, completion: nil)
}

// Prepare for next view.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destination1 = segue.destination as? ViewController2 {

        destination1.path = path

        if let index = sender as? Int {
            destination1.index = index
        }
    }
}

我的第二个 viewController。

var path = ""
var index = 0


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    print(path)
    print(index)
}

【问题讨论】:

  • sender 永远不会是整数。我想你想要的是sender.tag as? Int
  • 我认为错误不在 Int 值中,因为这段代码不应该崩溃。我认为这与你的用户界面有关
  • 您不应该手动调用 prepare prepareForSegue:sender:。一旦启动故事板转场,就会自动调用它

标签: ios swift swift3 segue


【解决方案1】:

这里有几个问题:Sender 应该是触发事件的对象。在大多数情况下,但不是全部情况下,它应该是self

第二个是您没有检查您的 segue 标识符。 prepareForSegue 调用中的所有内容都应包含在 if( segue.identifier == )` 块中。

第三,路径似乎没有在您显示的函数中声明,因此除非它出现在其他地方,否则可能会导致意外行为。

第四,由于您尝试将发件人用作 int,因此它正在扔东西。我假设你有一个声明的路径属性。您可能还应该为索引声明一个,并传递它而不是尝试使用发件人。然后您也不必将其包装在类型检查中。

您可能还会看到一个问题,因为您没有在第二个视图控制器上声明路径和索引的类型。为了让第一个视图控制器知道允许分配哪些类型,您可能需要这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多