我有2个控制器。他们每个人在屏幕上只有一个按钮。对于第一控制器按钮呈现模态第二控制器,并且对于第二控制器按钮正在解除呈现的控制器。
第一个代码很简单,如门:
class ViewController: UIViewController {
@IBAction func present(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
if let controller = storyboard.instantiateViewControllerWithIdentifier("web") as? UIViewController {
controller.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.presentViewController(controller, animated: true, completion: nil)
}
}
}
第二个控制器代码包含一些额外的代码,用于检测解除控制器后仍然存在。
static var index: Int = 0
var index: Int
required init(coder aDecoder: NSCoder) {
self.index = WebViewController.index++
super.init(coder: aDecoder)
let timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "action", userInfo: nil, repeats: true)
}
func action() {
NSLog("Class \(self.dynamicType) index is \(self.index)")
}
第二个控制器的所有代码如下:
class WebViewController: UIViewController {
static var index: Int = 0
var index: Int
required init(coder aDecoder: NSCoder) {
self.index = WebViewController.index++
super.init(coder: aDecoder)
let timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "action", userInfo: nil, repeats: true)
}
func action() {
NSLog("Class \(self.view) index is \(self.index)")
}
@IBAction func dismissSelf() {
if let presentingController = self.presentingViewController {
presentingController.dismissViewControllerAnimated(true, completion: nil)
}
}
}
因此,当您第一次运行此按钮并按第一个控制器屏幕上的按钮时,您将在控制台中看到每5秒钟:
类Proj.WebViewController索引为0
但如果您将解雇控制器并再次出现,那么您将看到两者:
类Proj.WebViewController索引为0
类Proj.WebViewController索引为1
据我所知,即使没有人从他身边抓住他,解雇并不是从记忆中移除控制器。
有人知道它是什么,我该如何解决?
您也可以下载sample project
答案 0 :(得分:3)
计时器正在捕捉你的视图控制器
您应该将 弱引用 保留给计时器,并在timer.invalidate()
dismissSelf
答案 1 :(得分:1)
在swift中,如果您的对象被利用,那么编译器会理解它将被使用,所以即使您dismissViewControllerAnimated
UIViewController
。它不是dealloc
。因此,在invalidate()
被解雇之前,您需要dismissViewControllerAnimated
计时器。此处dealloc
将替换为deinit
。
@IBAction func dismissSelf() {
timer.invalidate()
self.dismissViewControllerAnimated(true, completion: nil)
}
deinit {
self.view.removeFromSuperview()
println("call")
// perform the deinitialization
}
希望这对你有所帮助。
答案 2 :(得分:0)
了解更多
swift-nstimer-tutorial-lets-create-a-counter-application &安培; simple-stopwatch-app-in-swift
使用此...
@IBAction func dismissSelf() {
if let presentingController = self.presentingViewController {
timer.invalidate()// add this
presentingController.dismissViewControllerAnimated(true, completion: nil)
}
}
答案 3 :(得分:0)
我也遇到过这样的情况:“为什么关闭视图控制器不能从内存中清除显示的控制器?”。所以就我而言
FirstViewController: UIViewController {
var secondViewController: SecondViewController
override func viewDidLoad() {
super.viewDidLoad()
//Important...Don't Do instantiation here....
//Instantiate only when it's going to be used
secondViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
}
@IBAction fun buttonAction() {
self.present(secondViewController, animated: true)
}
}
在我的 SecondViewController
上,以上代码未调用deinit
对我有用的修复方法:
FirstViewController: UIViewController {
var secondViewController: SecondViewController
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction fun buttonAction() {
//Do the instantiation here
secondViewController = UIStoryboard.init(name: "Main", bundle: nil)
.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
self.present(secondViewController, animated: true)
//setting the view controller reference variable to nil
self.secondViewController = nil
}
}
SecondViewController的引用将保留在SecondViewController变量上,直到对其重新初始化或设置为nil为止。