You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关闭Popover后Navigation Item消失问题求助

Hey there! Let's break down why your navigation item is vanishing after closing the popover, and fix it step by step.

What's Going Wrong in Your Current Code?

Looking at your snippet, you're directly presenting the AniversarioViewController from within the popover before dismissing the popover itself. This creates a messy view controller stack: the new VC is presented on top of the popover, not your original navigation controller's stack. When you close everything, the original VC's navigation bar can get confused because the popover wasn't properly dismissed first, leading to it not rendering correctly.

Fix 1: Dismiss Popover First, Then Present the New VC

The simplest fix is to dismiss the popover first, and then present your target VC in the dismissal completion block. This ensures the popover is fully removed from the stack before you navigate:

// Save your data first
Shared.shared.filialNome = self.filiais[indexPath.row].razaoSocial
let selectedCodigo = self.filiais[indexPath.row].codigo
Shared.shared.codigoFilial = selectedCodigo

// Dismiss the popover first
self.dismiss(animated: true) {
    // Now present the new VC from the original presenting controller
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let newViewController = storyBoard.instantiateViewController(withIdentifier: "AniversarioViewController") as! AniversarioViewController
    
    // Use the original presenting controller (likely your navigation controller) to present
    if let navController = self.presentingViewController as? UINavigationController {
        navController.present(newViewController, animated: true, completion: nil)
    } else {
        self.presentingViewController?.present(newViewController, animated: true, completion: nil)
    }
}

Fix 2: Use Delegate/Closure to Let the Original VC Handle Navigation

A cleaner, more maintainable approach is to use a delegate or closure to notify your original view controller when a list item is selected. This keeps navigation logic in the parent VC and avoids stack confusion:

Step 1: Define a Delegate Protocol in Your Popover VC

protocol FilialSelectionDelegate: AnyObject {
    func didSelectFilial(nome: String, codigo: String)
}

class YourPopoverViewController: UIViewController {
    weak var delegate: FilialSelectionDelegate?
    var filiais: [YourFilialModel] = [] // Replace with your actual model type

    // Table view selection handler
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedFilial = filiais[indexPath.row]
        // Notify the delegate of the selection
        delegate?.didSelectFilial(nome: selectedFilial.razaoSocial, codigo: selectedFilial.codigo)
        // Dismiss the popover immediately
        self.dismiss(animated: true, completion: nil)
    }
}

Step 2: Implement the Delegate in Your Original VC

class YourOriginalViewController: UIViewController, FilialSelectionDelegate {
    // Code to open the popover
    func openFilialPopover() {
        let popoverVC = storyboard?.instantiateViewController(withIdentifier: "YourPopoverVC") as! YourPopoverViewController
        popoverVC.delegate = self
        popoverVC.modalPresentationStyle = .popover
        // Set popover anchor (adjust sourceView/sourceRect to match your trigger element)
        if let popoverPresentationController = popoverVC.popoverPresentationController {
            popoverPresentationController.sourceView = self.yourTriggerButton // Replace with your button/view
            popoverPresentationController.sourceRect = self.yourTriggerButton.bounds
        }
        present(popoverVC, animated: true, completion: nil)
    }

    // Delegate method to handle the selection and navigation
    func didSelectFilial(nome: String, codigo: String) {
        // Save data to your Shared singleton
        Shared.shared.filialNome = nome
        Shared.shared.codigoFilial = codigo

        // Navigate to AniversarioViewController
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "AniversarioViewController") as! AniversarioViewController
        
        // If your original VC is in a navigation stack, use push instead of present for smoother flow
        self.navigationController?.pushViewController(newViewController, animated: true)
        // Or keep present if that's your intended behavior:
        // self.present(newViewController, animated: true, completion: nil)
    }
}

Bonus Check: Verify Navigation Bar Settings

Make sure AniversarioViewController isn't accidentally hiding the navigation bar:

  • In Interface Builder: Ensure the "Navigation Bar" is enabled in the VC's attributes inspector.
  • In code: Double-check that you don't have navigationController?.isNavigationBarHidden = true in viewWillAppear or viewDidLoad of either the original VC or AniversarioViewController.

内容的提问来源于stack exchange,提问作者Junior Sanches

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:18:16