简体   繁体   English

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

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

I have issue where my app has a screen that scans a UPC code and shows the product detail screen if a match found, however the screen shows up as a modal and there is no back button, it looks like this and if I make it full screen you can't leave this screen.我有一个问题,我的应用程序有一个屏幕可以扫描 UPC 代码并在找到匹配项时显示产品详细信息屏幕,但是屏幕显示为模态并且没有后退按钮,它看起来像这样,如果我把它填满screen 你不能离开这个屏幕。

Screen without back button没有后退按钮的屏幕

Here is it looks when accessing via the search screen这是通过搜索屏幕访问时的样子

Here is how it should look with back button这是带有后退按钮的外观

Here is the code I'm using the present the product detail 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)
    
}

Any help to get the back button on the screen would be greatly appreciated任何帮助在屏幕上获得后退按钮的帮助将不胜感激

You can use pushViewController approach instead of present:您可以使用 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)

Hope this helps!希望这可以帮助!

#Method 1 #方法一

From Apple documentation regarding to pushViewController(_:animated:) Link :来自关于pushViewController(_:animated:) 链接的 Apple 文档:

In addition to displaying the view associated with the new view controller at the top of the stack, this method also updates the navigation bar and tool bar accordingly.除了在堆栈顶部显示与新视图 controller 关联的视图外,此方法还相应地更新导航栏和工具栏。

To update your navigation bar and tool bar in order to show the back button you should push the viewController from your existing navigation controller:要更新导航栏和工具栏以显示后退按钮,您应该从现有导航controller中推送 viewController:

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

instead of:代替:

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

#Method 2 #方法二

To dismiss a full screen presented ViewController simply add a UIBarButtonItem to the navigationItem:要关闭全屏呈现的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