当我以编程方式继续时,UIBarButtonItem 不会显示
UIBarButtonItem will not show when I segue programmatically
我正在构建一个应用程序,我在其中以编程方式从 ViewControllerA 进行转场,因为它是一个菜单项列表,每个菜单项都会导致不同的 VC,因此我没有多个转场,而是在代码中实例化视图控制器。我的问题是,即使我的故事板上有一个栏按钮项目,它也不会显示在应用程序中。
查看控制器 A 菜单模型代码:
class PlayerMenu: Hashable {
let title: String
let numberOfItems: String
let menuItemViewController: UIViewController.Type?
init(title: String, numberOfItems: String, viewController: UIViewController.Type? = nil) {
self.title = title
self.numberOfItems = numberOfItems
self.menuItemViewController = viewController
}
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
static func == (lhs: PlayerMenu, rhs: PlayerMenu) -> Bool {
return lhs.identifier == rhs.identifier
}
private let identifier = UUID()
}
ViewController一个代码
private lazy var menuItems: [PlayerMenu] = {
return [
PlayerMenu(title: "Status", numberOfItems: "Available.", viewController: nil),
PlayerMenu(title: "Pertinent Medical History", numberOfItems: "", viewController: nil),
PlayerMenu(title: "Injury / Illness", numberOfItems: "There are no injuries.", viewController: nil),
PlayerMenu(title: "Allergy", numberOfItems: "There are no allergies.", viewController: AllergyTableViewController.self),
PlayerMenu(title: "Neurocognitive", numberOfItems: "Unavailable.", viewController: nil),
PlayerMenu(title: "Medication", numberOfItems: "There are no medications.", viewController: nil),
PlayerMenu(title: "Immunization", numberOfItems: "There are no immunizations", viewController: nil),
PlayerMenu(title: "Dictations", numberOfItems: "There are no dictations.", viewController: nil),
PlayerMenu(title: "Imaging", numberOfItems: "There is no imaging.", viewController: nil),
PlayerMenu(title: "Participation", numberOfItems: "Unavailable.", viewController: nil),
PlayerMenu(title: "Away Game Injury", numberOfItems: "Unavailable.", viewController: nil),
]
}()
ViewController转场
extension PlayerDashboardViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let menuItem = self.dataSource.itemIdentifier(for: indexPath) else { return }
if let viewController = menuItem.menuItemViewController {
let navController = UINavigationController(rootViewController: viewController.init())
present(navController, animated: true)
}
}
}
故事板截图
您没有看到您在情节提要中设计的视图控制器的原因是调用 viewController.init()
不会从您的情节提要中实例化您的视图控制器。您需要致电:
UIStoryboard.instantiateViewController(withIdentifier:)
或者(仅适用于 iOS 13+):
UIStoryboard.instantiateViewController(identifier:creator:)
我建议不要将视图控制器类型存储在 PlayerMenu
class 中,而是存储视图控制器的故事板标识符,如下所示:
class PlayerMenu: Hashable {
let title: String
let numberOfItems: String
let menuItemViewControllerStoryboardIdentifier: String?
init(title: String, numberOfItems: String, viewControllerStoryboardIdentifier: String? = nil) {
self.title = title
self.numberOfItems = numberOfItems
self.menuItemViewControllerStoryboardIdentifier = viewControllerStoryboardIdentifier
}
[...]
}
然后当你想实例化你的view controller时,获取你的storyboard的引用(如果它与当前view controller的storyboard相同,那么你可以只说self.storyboard
,否则直接创建storyboard UIStoryboard.init(name:bundle:)
):
extension PlayerDashboardViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let menuItem = self.dataSource.itemIdentifier(for: indexPath),
let viewControllerStoryboardIdentifier = menuItem.menuItemViewControllerStoryboardIdentifier,
let viewController = self.storyboard?.instantiateViewController(withIdentifier: viewControllerStoryboardIdentifier) else {
return
}
let navController = UINavigationController(rootViewController: viewController)
present(navController, animated: true)
}
}
然后确保在故事板中提供视图控制器标识符,并像这样创建 PlayerMenu
项目:
PlayerMenu(title: "Allergy", numberOfItems: "There are no allergies.", viewControllerStoryboardIdentifier: "AllergyTableViewController (or whatever identifier you give this in your storyboard)")
我正在构建一个应用程序,我在其中以编程方式从 ViewControllerA 进行转场,因为它是一个菜单项列表,每个菜单项都会导致不同的 VC,因此我没有多个转场,而是在代码中实例化视图控制器。我的问题是,即使我的故事板上有一个栏按钮项目,它也不会显示在应用程序中。
查看控制器 A 菜单模型代码:
class PlayerMenu: Hashable {
let title: String
let numberOfItems: String
let menuItemViewController: UIViewController.Type?
init(title: String, numberOfItems: String, viewController: UIViewController.Type? = nil) {
self.title = title
self.numberOfItems = numberOfItems
self.menuItemViewController = viewController
}
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
static func == (lhs: PlayerMenu, rhs: PlayerMenu) -> Bool {
return lhs.identifier == rhs.identifier
}
private let identifier = UUID()
}
ViewController一个代码
private lazy var menuItems: [PlayerMenu] = {
return [
PlayerMenu(title: "Status", numberOfItems: "Available.", viewController: nil),
PlayerMenu(title: "Pertinent Medical History", numberOfItems: "", viewController: nil),
PlayerMenu(title: "Injury / Illness", numberOfItems: "There are no injuries.", viewController: nil),
PlayerMenu(title: "Allergy", numberOfItems: "There are no allergies.", viewController: AllergyTableViewController.self),
PlayerMenu(title: "Neurocognitive", numberOfItems: "Unavailable.", viewController: nil),
PlayerMenu(title: "Medication", numberOfItems: "There are no medications.", viewController: nil),
PlayerMenu(title: "Immunization", numberOfItems: "There are no immunizations", viewController: nil),
PlayerMenu(title: "Dictations", numberOfItems: "There are no dictations.", viewController: nil),
PlayerMenu(title: "Imaging", numberOfItems: "There is no imaging.", viewController: nil),
PlayerMenu(title: "Participation", numberOfItems: "Unavailable.", viewController: nil),
PlayerMenu(title: "Away Game Injury", numberOfItems: "Unavailable.", viewController: nil),
]
}()
ViewController转场
extension PlayerDashboardViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let menuItem = self.dataSource.itemIdentifier(for: indexPath) else { return }
if let viewController = menuItem.menuItemViewController {
let navController = UINavigationController(rootViewController: viewController.init())
present(navController, animated: true)
}
}
}
故事板截图
您没有看到您在情节提要中设计的视图控制器的原因是调用 viewController.init()
不会从您的情节提要中实例化您的视图控制器。您需要致电:
UIStoryboard.instantiateViewController(withIdentifier:)
或者(仅适用于 iOS 13+):
UIStoryboard.instantiateViewController(identifier:creator:)
我建议不要将视图控制器类型存储在 PlayerMenu
class 中,而是存储视图控制器的故事板标识符,如下所示:
class PlayerMenu: Hashable {
let title: String
let numberOfItems: String
let menuItemViewControllerStoryboardIdentifier: String?
init(title: String, numberOfItems: String, viewControllerStoryboardIdentifier: String? = nil) {
self.title = title
self.numberOfItems = numberOfItems
self.menuItemViewControllerStoryboardIdentifier = viewControllerStoryboardIdentifier
}
[...]
}
然后当你想实例化你的view controller时,获取你的storyboard的引用(如果它与当前view controller的storyboard相同,那么你可以只说self.storyboard
,否则直接创建storyboard UIStoryboard.init(name:bundle:)
):
extension PlayerDashboardViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let menuItem = self.dataSource.itemIdentifier(for: indexPath),
let viewControllerStoryboardIdentifier = menuItem.menuItemViewControllerStoryboardIdentifier,
let viewController = self.storyboard?.instantiateViewController(withIdentifier: viewControllerStoryboardIdentifier) else {
return
}
let navController = UINavigationController(rootViewController: viewController)
present(navController, animated: true)
}
}
然后确保在故事板中提供视图控制器标识符,并像这样创建 PlayerMenu
项目:
PlayerMenu(title: "Allergy", numberOfItems: "There are no allergies.", viewControllerStoryboardIdentifier: "AllergyTableViewController (or whatever identifier you give this in your storyboard)")