繁体   English   中英

无法将后退按钮添加到 swift 导航 controller

[英]Not able to add back button to swift navigation controller

我有一个问题,我的应用程序有一个屏幕可以扫描 UPC 代码并在找到匹配项时显示产品详细信息屏幕,但是屏幕显示为模态并且没有后退按钮,它看起来像这样,如果我把它填满screen 你不能离开这个屏幕。

没有后退按钮的屏幕

这是通过搜索屏幕访问时的样子

这是带有后退按钮的外观

这是我在产品详细信息屏幕上使用的代码:

func launchApp(decodedURL: String) {
    
    if presentedViewController != nil {
        return
    }
    
    let decodedUPC = Int(decodedURL)
    
    guard let goodProduct = ProductsProvider().queryUPC(upcNumber: decodedUPC!) else  {
        
        let alertController = UIAlertController(title: "UPC Code not found \(String(describing: decodedUPC))", message: "We could not find this item, please use our search", preferredStyle: .alert)
        
        let confirmAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { (action) -> Void in
            
            // Code here
            self.dismiss(animated: true, completion: nil)
            
        })
        
        alertController.addAction(confirmAction)
        
        present(alertController, animated: true, completion: nil)
        return
        
    }
            

    let viewController:
        ProductDetailView = UIStoryboard(
            name: "Main", bundle: Bundle(for: ProductDetailView.self)
            ).instantiateViewController(withIdentifier: "productDetailViewController") as! ProductDetailView
    
    viewController.productDetail = goodProduct
    viewController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
    viewController.navigationItem.leftItemsSupplementBackButton = true
    
    
    let navigationController = UINavigationController(rootViewController: viewController)
    // navigationController.modalPresentationStyle = .fullScreen

    

    self.present(navigationController, animated: false, completion: nil)
    
}

任何帮助在屏幕上获得后退按钮的帮助将不胜感激

您可以使用 pushViewController 方法代替 present:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "identifier_of_target_vc") as! ClassNameofTargetVC 
self.navigationController?.pushViewController(nextViewController, animated:true)

希望这可以帮助!

#方法一

来自关于pushViewController(_:animated:) 链接的 Apple 文档:

除了在堆栈顶部显示与新视图 controller 关联的视图外,此方法还相应地更新导航栏和工具栏。

要更新导航栏和工具栏以显示后退按钮,您应该从现有导航controller中推送 viewController:

self.navigationController?.pushViewController(viewController, animated: false)

代替:

let navigationController = UINavigationController(rootViewController: viewController)
self.present(navigationController, animated: false, completion: nil)

#方法二

要关闭全屏呈现的ViewController ,只需将UIBarButtonItem添加到 navigationItem:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Presented View"

        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelAction))
    }

    @objc func cancelAction() {
        dismiss(animated: true)
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM