viewController没有被正确删除? [英] viewController not being removed properly?
问题描述
在我的viewController中,我有一个 NSFetchedResultsController
用于填充 tableView
.在 viewDidLoad()
中,我将当前时间保存到变量中,然后在 NSFetchedResultsController的
didChange AnyObject
回调中将其打印出来:>
In my viewController I have an NSFetchedResultsController
for populating a tableView
. In the viewDidLoad()
I'm saving the current time to a variable, then printing it out in the NSFetchedResultsController's
didChange AnyObject
callback:
class MyViewController: UIViewController, NSFetchedResultsControllerDelegate {
var date: Date!
var fetchedResultsController: NSFetchedResultsController<Event>!
override func viewDidLoad() {
super.viewDidLoad()
date = Date()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
feedTable.insertRows(at: [newIndexPath!], with: .fade)
print(date)
default:
break
}
}
我注意到,当我离开该视图然后返回时,此回调被调用两次,并打印了我第一次进入该视图时的日期以及新的视图:
I noticed that when I leave this view then come back this callback is called twice and the date from the first time I was in the view is printed as well as the new one:
2017-05-14 18:29:42 +0000
2017-05-14 18:29:42 +0000
2017-05-14 18:30:01 +0000
2017-05-14 18:30:01 +0000
我将视图留在这样的单独函数中:
I'm leaving the view in a seperate function like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "HomePage") as? HomeVC
controller?.managedObjectContext = self.managedObjectContext
self.present(controller!, animated: true, completion: nil)
为什么即使我离开并回来后,这个 MyViewController
似乎仍然存在?我离开它的方式是否无法正确删除?
Why does this MyViewController
seem to stick around even after I leave it and come back? Is the way I'm leaving it not removing it properly?
推荐答案
我将视图留在这样的单独函数中:
I'm leaving the view in a seperate function like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "HomePage") as? HomeVC
controller?.managedObjectContext = self.managedObjectContext
self.present(controller!, animated: true, completion: nil)
是的,就是这个问题.而不是返回到现有的HomeVC,而是创建一个新 HomeVC,并在您自己的上方显示 .因此,您只需不断堆积视图控制器即可.
Yup, that's the problem. Instead of returning to the existing HomeVC, you're making a new HomeVC and presenting that on top of yourself. So you just keep piling up the view controllers.
您需要做的就是回到家,放松一下在这里所做的事情.如果存在
,则现在关闭
.如果是 push
,现在是 pop
.
What you need to do to get back home is unwind what you did to get here. If present
, now dismiss
. If push
, now pop
.
这篇关于viewController没有被正确删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!