gpt4 book ai didi

ios - 当我以编程方式进行 segue 时,UIBarButtonItem 不会显示

转载 作者:行者123 更新时间:2023-12-01 21:59:57 26 4
gpt4 key购买 nike

我正在构建一个应用程序,我在其中以编程方式从 ViewControllerA 进行 segue,因为它是一个菜单项列表,每个菜单项都指向不同的 VC,因此我没有使用多个 segue,而是在代码中实例化 View Controller 。我的问题是,即使我的 Storyboard上有一个栏按钮项目,它也不会显示在应用程序中。

View Controller 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 A 代码
 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),

]
}()

ViewControllerA Segue
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)
}
}
}

Storyboard截图
enter image description here

最佳答案

您没有看到您在 Storyboard中设计的 View Controller 的原因是因为调用 viewController.init()不会从 Storyboard 中实例化您的 View Controller 。您需要调用:

UIStoryboard.instantiateViewController(withIdentifier:)

或者(仅适用于 iOS 13+):

UIStoryboard.instantiateViewController(identifier:creator:)

我建议不要将 View Controller 类型存储在 PlayerMenu 中。存储 View Controller 的 Storyboard标识符的类,如下所示:

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 ,否则直接使用 UIStoryboard.init(name:bundle:) 创建 Storyboard):

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)
}
}

然后确保在 Storyboard 中提供 View Controller 标识符,并创建 PlayerMenu像这样的项目:

PlayerMenu(title: "Allergy", numberOfItems: "There are no allergies.", viewControllerStoryboardIdentifier: "AllergyTableViewController (or whatever identifier you give this in your storyboard)")

关于ios - 当我以编程方式进行 segue 时,UIBarButtonItem 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60289549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com