![](/img/trans.png)
[英]storyboard?.instantiateViewController(withIdentifier: “ ”) as! viewcontroller
[英]Swift iOS -storyboard.instantiateViewController(withIdentifier:) causing retain cycle
我使用的是我的物理设备,而不是模拟器。
我正在使用storyboard.instantiateViewController(withIdentifier:)
实例化一个vc并以模态形式呈现它。 我使用presentingViewController?.dismiss(animated: true, completion: nil)
将其关闭。 在显示的vc中,我在Deinit
中有一个永远不会运行的打印方法。
我去了仪器>分配>统计信息>分配摘要> MyApp.ThePresenedController ,它显示了2张面孔,表示有问题。 当我单击它们时,将我带到呈现vc的代码,在该代码中我实例化了vc以呈现并以绿色突出显示。 提出的vc被关闭后,它不会从“ 分配摘要”列表中删除。 在所呈现的vc中,没有对所呈现的vc的引用,因此它不是weak var
问题。
storyboard.instantiateViewController(withIdentifier:)
怎么导致我出现问题?
呈现VC:
@IBAction func forgotPasswordButtonTapped(_ sender: UIButton) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let forgotPasswordVC = mainStoryboard.instantiateViewController(withIdentifier: "ForgotPasswordController") as! ForgotPasswordController
let navVC = UINavigationController(rootViewController: forgotPasswordVC)
present(navVC, animated: true, completion: nil)
}
呈现的VC:
@IBAction func cancelButtonTapped(_ sender: UIButton) {
presentingViewController?.dismiss(animated: true, completion: nil)
}
deinit{
print("I've been dismissed")
}
我还在AppDelegate中使用相同的storyboard.instantiateViewController(withIdentifier:)
代码,并且出现了相同的2张面孔和突出显示的绿色错误。
AppDelegate didFinishLaunching:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if userDoesThis {
// if true this first line will highlight green
let thisVC: ThisController = mainStoryboard.instantiateViewController(withIdentifier: "ThisController") as! ThisController
let nav = UINavigationController(rootViewController: thisVC)
} else {
// if false this first line will highlight green
let thatVC: ThatController = mainStoryboard.instantiateViewController(withIdentifier: "ThisController") as! ThatController
let nav = UINavigationController(rootViewController: thatVC)
}
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
正如@StevenFisher在评论中建议的那样,问题不是绿色突出显示的行本身,而是我忽略的闭包,并且没有用[weak self]
声明。 我读过的文章说按不露脸的表情会将您带到令人讨厌的代码行,但是Steve指出这可能不是问题所在,它可以/将代替您进入问题的起点。 在我的情况下,它让我知道文件实例化后,文件中的某个地方出现了问题,而不是行本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.