Swift 呈现缓存的视图控制器时应用崩溃
Swift app crashes when presenting cached View Controller
我的 iOS 应用有两个 ViewController (VC)。当我按下 VC A 上的按钮时,它显示 VC B。当我按下 VC B 上的按钮时,它显示 VC A。这很好。但是,事实证明,每次切换时它都会为每个视图控制器创建一个新实例,这会扰乱我的应用程序的某些功能。我试图改变这个。
正如您在下面看到的,我只是在创建 VC 的新实例(如果实例不存在的话)。我在 VC B 中保存 VC A,反之亦然。
VC一个文件:
var vcB: BViewController?
func leftPresentVC() {
if(vcB == nil) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
vcB = storyboard.instantiateViewController(withIdentifier: "vcB") as! BViewController
vcB?.transitioningDelegate = transition
vcB?.modalPresentationStyle = .custom
vcB?.vcA = self
}
present(vcB!, animated: true, completion: {})
}
VC B档:
var vcA: AViewController?
func rightPresentVC() {
if(vcA == nil) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
vcA = storyboard.instantiateViewController(withIdentifier: "vcA") as! AViewController
vcA?.transitioningDelegate = transition
vcA?.modalPresentationStyle = .custom
}
if let vcA = vcA {
present(vcA, animated: true, completion: {})
}
}
函数 leftPresentVC 和 rightPresentVC 在按下按钮时被调用。
所以问题是这样的。当我启动应用程序时,VC A 按计划加载。我单击一个按钮,VC B 按计划显示。然后我单击一个按钮转到 VC A,应用程序崩溃了。我在 VC B 的 present(vcA, animated: true, completion: {})
中得到一个错误,说 EXC_BAD_ACCESS 并且它给出了一个内存地址。知道问题出在哪里吗?
为什么你要介绍一个相同的。
只要关闭 VCb,你就会得到 VCa
dismiss(true, completion: nil)
我的 iOS 应用有两个 ViewController (VC)。当我按下 VC A 上的按钮时,它显示 VC B。当我按下 VC B 上的按钮时,它显示 VC A。这很好。但是,事实证明,每次切换时它都会为每个视图控制器创建一个新实例,这会扰乱我的应用程序的某些功能。我试图改变这个。
正如您在下面看到的,我只是在创建 VC 的新实例(如果实例不存在的话)。我在 VC B 中保存 VC A,反之亦然。
VC一个文件:
var vcB: BViewController?
func leftPresentVC() {
if(vcB == nil) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
vcB = storyboard.instantiateViewController(withIdentifier: "vcB") as! BViewController
vcB?.transitioningDelegate = transition
vcB?.modalPresentationStyle = .custom
vcB?.vcA = self
}
present(vcB!, animated: true, completion: {})
}
VC B档:
var vcA: AViewController?
func rightPresentVC() {
if(vcA == nil) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
vcA = storyboard.instantiateViewController(withIdentifier: "vcA") as! AViewController
vcA?.transitioningDelegate = transition
vcA?.modalPresentationStyle = .custom
}
if let vcA = vcA {
present(vcA, animated: true, completion: {})
}
}
函数 leftPresentVC 和 rightPresentVC 在按下按钮时被调用。
所以问题是这样的。当我启动应用程序时,VC A 按计划加载。我单击一个按钮,VC B 按计划显示。然后我单击一个按钮转到 VC A,应用程序崩溃了。我在 VC B 的 present(vcA, animated: true, completion: {})
中得到一个错误,说 EXC_BAD_ACCESS 并且它给出了一个内存地址。知道问题出在哪里吗?
为什么你要介绍一个相同的。
只要关闭 VCb,你就会得到 VCa
dismiss(true, completion: nil)