gpt4 book ai didi

ios - 添加准备功能后导航栏消失

转载 作者:行者123 更新时间:2023-11-30 12:12:18 28 4
gpt4 key购买 nike

我正在使用录音应用程序。

我尝试在第一个记录 View Controller 中添加导航 Controller ,然后可以使用以下函数准备将文件名数组传递到第二个 View Controller :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as? MainViewController
controller?.recordArray = recordingArray
self.present(controller!, animated: true, completion: nil)
}

但是,当在模拟器中运行时,导航栏在第二个 Controller 中消失,并且 Xcode 弹出警告

Thread1: EXC_BAD_ACCESS(code=2,address=0x7fff51edfff8)

有人有什么建议吗?谢谢!

最佳答案

永远不要在 prepare(for segue:) 中调用 present(_:animated:completion:)

prepare(for segue) 在 Segue 即将发生之前由系统自动调用,让您准备发送到目标 View Controller 的数据或执行之前需要的任何其他计算执行segue。需要在 Storyboard 中设置 Segue,它会自动调用,或者如果是手动 Segue,您需要使用 perform(segue) 调用它,一旦您这样做,系统就会为您调用prepare(for segue)。您可以看到为什么调用另一个导航函数会出现问题,因为您正在尝试使用两种不同的方法(segue 和present)导航到另一个 View Controller 。

如果你还没有在Storyboard中设置segue,那么你也需要这样做,因为如果没有设置,letcontroller = segue.destination as? MainViewController 将为 nil

在 Storyboard 中设置 Segue 后,您的函数应如下所示:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? MainViewController {
controller.recordArray = recordingArray
self.present(controller, animated: true, completion: nil)
}
}

关于ios - 添加准备功能后导航栏消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45903291/

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