gpt4 book ai didi

ios - 使用 Segue 传递整数

转载 作者:行者123 更新时间:2023-11-28 15:36:42 25 4
gpt4 key购买 nike

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

destination1.path = path

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

我收到此错误消息:

2017-05-26 20:37:14.442509+0200 Subscription[1224:301622] UIView: 0x105d294f0; frame = (0 0; 414 736); autoresize = W+H; layer = CALayer: 0x17403f360>>'s window is not equal to Subscription.ViewController2: 0x105d3fcb0>'s view's window!

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

最佳答案

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

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

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

第四,由于您试图将 sender 用作 int,所以它会把事情搞砸。我假设您有一个声明路径的属性。您可能还应该为索引声明一个,然后传递它而不是尝试使用 sender。您也不必将其包装在类型检查中。

您可能还会遇到一个问题,因为您没有在第二个 View Controller 上声明路径和索引的类型。您可能需要这样做,以便第一个 View Controller 知道允许分配哪些类型。

关于ios - 使用 Segue 传递整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44208441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com