(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)
我想展示 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)