将自定义视图控制器推入UIDocumentBrowserViewController的导航堆栈中

时间:2019-05-01 03:14:18

标签: swift uinavigationcontroller uibarbuttonitem uidocumentbrowservc

我正在尝试向UIDocumentBrowserViewController添加自定义栏按钮项。我可以使用数组属性additionalLeadingNavigationBarButtonItems添加自定义栏项目。我可以在运行应用程序时触发此按钮,但是,此外,当我触发按钮时,我想将新的视图控制器推入UIDocumentBrowserViewController的导航堆栈中。有一个UIDocumentBrowserViewController。navigationController属性,但是当我运行该程序时此属性为nil。有没有正确的方法来访问与UIDocumentBrowserViewController关联的navigationController以及从条形按钮推入新视图控制器的正确方法是什么?谢谢

override func viewDidLoad() {
    super.viewDidLoad()
    delegate = self
    allowsDocumentCreation = true
    allowsPickingMultipleItems = false

    let barButton = UIBarButtonItem.init(title: "⚙︎", style: .plain, target: self, action: #selector(browserSettings))

    additionalLeadingNavigationBarButtonItems = [barButton]
}

@objc func browserSettings() {
    print("in browserSettings")
    let settingViewController = HelpViewController()
    settingViewController.setupView()
    settingViewController.textView.text = "hello"
    settingViewController.textView.contentOffset = CGPoint.zero
    let navController = self.navigationController
    navController?.pushViewController(settingViewController, animated: true)
    //navController is nil??
}

当我触发按钮时,程序仅打印 “在browserSettings中” navController为零

1 个答案:

答案 0 :(得分:0)

尝试一下

@objc func browserSettings() {
    print("in browserSettings")
    let settingViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "StoryboardID") as! HelpViewController
    settingViewController.textView.text = "hello"
    settingViewController.textView.contentOffset = CGPoint.zero
    self.navigationController?.pushViewController(settingViewController, animated: true)
}

并将Navigation Controller添加到UIViewController中的Main.Storyboard

Navigation Controller