簡體   English   中英

(Swift) 導航欄隱藏在 viewController 中

[英](Swift) Navigation bar is hidden in viewController

我想從 FirstVC 展示 SecondVC 並使 SecondVC 有一個稱為Close的 rightBarButtonItem,它調用一個@objc函數來關閉 SecondVC。
另外,我想從 firstVC 更改 secondVC 的標題:
這就是我從 FirstVC 介紹 SecondVC 的方式:

let secondVC = AdviceDetailsViewController()
secondVC.modalPresentationStyle = .fullScreen
secondVC.title = "Example" //Value of type 'UINavigationController' has no member 'myTitle'
self.present(secondVC, animated: true)

secondVC 中導航欄的代碼:

public var myTitle: String = ""
override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .gray
    self.title = myTitle
    self.navigationController?.navigationBar.barTintColor = UIColor.orange
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(closeDetails))
}

@objc func closeDetails() {
    self.dismiss(animated: true, completion: nil)
}

在 secondVC 中看不到導航欄,唯一可見的是灰色背景色。
我應該改變什么? 我在這個應用程序中以編程方式做所有事情。

使用此方法顯示第二個 VC

let nc = UINavigationController(rootViewController: AdviceDetailsViewController())
nc.modalPresentationStyle = . fullScreen
self.present(nc, animated: true)

結果

關於您的標題問題, “UINavigationController”類型的值沒有成員“myTitle”是的,因為我們在 SecondVC 上定義了 myTitle 變量,所以我們像這樣分配 myTitle

let vc = SecondViewController()
vc.myTitle = "My Title"
let nc = UINavigationController(rootViewController: vc)
nc.modalPresentationStyle = . fullScreen
self.present(nc, animated: true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM