((Swift)导航栏被隐藏在viewController中

问题描述 投票:0回答:2

我想从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)

secondaryVC中的navigationBar的代码:

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中没有导航栏可见,唯一可见的是灰色背景色。我应该改变什么?我正在此应用中以编程方式进行所有操作。

ios swift uiviewcontroller uinavigationbar uinavigationitem
2个回答
1
投票

使用此方法显示第二个VC

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

Result


1
投票

关于您的标题问题,类型'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)
© www.soinside.com 2019 - 2024. All rights reserved.