简体   繁体   English

Swift:解除分配模式呈现的视图 controller

[英]Swift: deallocate modally presented view controller

I have a modally presented SearchviewController that contains a UISearchController.我有一个模态呈现的 SearchviewController,其中包含一个 UISearchController。 When swiping down it gets deallocated, but only if the searchControllers searchBar is not in editing mode.向下滑动时,它会被释放,但前提是 searchControllers searchBar 未处于编辑模式。 Only if I press its cancel button in advance, it gets deallocated.只有当我提前按下它的取消按钮时,它才会被释放。 How can I make sure it gets deallocated, even when in editing mode?即使在编辑模式下,如何确保它被释放? There are definitely no strong self references within any closures...在任何闭包中绝对没有强大的自我引用......

Presenting ViewController:呈现 ViewController:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    addButton()
}

func addButton() {
    let mediumConfiguration = UIImage.SymbolConfiguration(scale: .large)
    var checkButtonImage = UIImage(systemName: "plus", withConfiguration: mediumConfiguration)
    checkButtonImage = checkButtonImage?.withTintColor(.label)
    
    let button = UIButton(type: .contactAdd)
    button.addTarget(self, action: #selector(onAddViewControllerButtonClicked(sender:)), for: .touchUpInside)
    
    view.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
}
@objc func onAddViewControllerButtonClicked(sender: UIButton) {
    
    let viewController = SearchViewController()
    viewController.view.backgroundColor = .secondarySystemBackground
    
    let navigationController = UINavigationController()
    navigationController.viewControllers = [viewController]
    
    self.present(navigationController, animated: true)
}

} }

Presented ViewController:呈现的 ViewController:

class SearchViewController: UIViewController, UISearchBarDelegate, UISearchResultsUpdating {

override func viewDidLoad() {
    super.viewDidLoad()
    configureSearchController()
}

var searchController: UISearchController?
func configureSearchController() {
    //search
    searchController = UISearchController(searchResultsController: nil)
    searchController?.searchResultsUpdater = self
    searchController?.searchBar.delegate = self
    searchController?.hidesNavigationBarDuringPresentation = false
    searchController?.searchBar.searchBarStyle = .minimal
    searchController?.searchBar.keyboardType = .webSearch
    self.navigationItem.searchController?.searchBar.backgroundColor = .clear
    self.navigationItem.searchController = searchController
    self.navigationItem.hidesSearchBarWhenScrolling = false
    self.definesPresentationContext = true
    self.navigationItem.searchController = searchController
}
func updateSearchResults(for searchController: UISearchController) {
    return
}

//check deallocation
deinit { print("\(NSStringFromClass(type(of: self))): deallocated") }

} }

Can you help with that?你能帮忙吗?

Thank you in advance!先感谢您!

Adding添加

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    self.navigationItem.searchController = nil
}

to SearchViewController fixes the problem for me, but admittedly I have no idea as to why this is necessary.SearchViewController为我解决了这个问题,但我承认我不知道为什么这是必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:轻按Tab栏时关闭模态呈现的视图控制器 - Swift: Dismissing Modally Presented View Controller when Tab Bar is tapped Swift尝试以模态形式呈现一个活动控制器[Current view controller],即使所呈现的控制器是另一个 - Swift tried to present modally an active controller [Current view controller] even though the presented controller is another one 在 Swift 5 中模态呈现的视图上显示标题 - Display title on modally presented view in Swift 5 从模态呈现的视图控制器导航到根视图控制器 - Navigate to root view-controller from modally presented view controller 取消双模态呈现视图控制器的父视图控制器 - Dismissing parent view controller of double modally presented view controller 关闭从模态呈现的视图控制器翻转的视图控制器 - Dismiss view controller flipped from modally presented view controller 动画根视图 controller 嵌入到导航 controller 以模态方式呈现 - Animate root view controller embedded into a navigation controller presented modally 委托从模态呈现的视图控制器传回数据 - Delegation to pass data back from modally presented view controller iOS 关闭模态呈现视图时黑屏 controller - iOS Black screen when close modally presented view controller 如何检查视图控制器是模态呈现还是推送到导航堆栈上? - How to check if a view controller is presented modally or pushed on a navigation stack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM