【问题标题】:Why does this not open a navigation controller window?为什么这不打开导航控制器窗口?
【发布时间】:2026-02-13 11:55:01
【问题描述】:

为什么下面的代码在按下相机按钮时没有打开导航控制器? done 函数指定相机应该推送视图控制器但没有任何反应。

func setNavigationBar() {
    let screenSize: CGRect = UIScreen.main.bounds
    let navBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: screenSize.width, height: 160))
    let navItem = UINavigationItem(title: "Hello")
    let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: nil, action: #selector(done))
    navItem.rightBarButtonItem = doneItem
    navBar.setItems([navItem], animated: false)
    self.view.addSubview(navBar)
}

@objc func done() { // remove @objc for Swift 3
    let dummyViewController = UIViewController()
    navigationController?.pushViewController(dummyViewController, animated: true)
}

这是 AppDelagate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:

    [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    Thread.sleep(forTimeInterval: 2.0)
    FirebaseApp.configure()

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    window?.rootViewController =
        UINavigationController(rootViewController: MessagesController())
    UITableViewCell.appearance().textLabel?.textColor = UIColor.white;
    UITableViewCell.appearance().backgroundColor = UIColor.clear
    UIApplication.shared.statusBarStyle = .lightContent
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
    UINavigationBar.appearance().barTintColor = UIColor(red:0.13, green:0.15, blue:0.18, alpha:1.0)

    UIApplication.shared.isStatusBarHidden = false






    return true

}


func applicationWillResignActive(_ application: UIApplication) {
    let im = UIImageView()

    im.tag = 12

    im.frame = (self.window?.frame)!

    im.image = UIImage.init(named: "launchbackground.png")

    application.keyWindow?.subviews.last?.addSubview(im)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    let im = UIImageView()

    im.tag = 12

    im.frame = (self.window?.frame)!

    im.image = UIImage.init(named: "launchbackground.png")

    application.keyWindow?.subviews.last?.addSubview(im)
}

func applicationWillEnterForeground(_ application: UIApplication) {
    if let sub = application.keyWindow?.subviews.last
    {
        for vv in sub.subviews
        {
            if(vv.tag == 12)
            {
                vv.removeFromSuperview()
            }

        }

    }

}


func applicationDidBecomeActive(_ application: UIApplication) {
    if let sub = application.keyWindow?.subviews.last
    {
        for vv in sub.subviews
        {
            if(vv.tag == 12)
            {
                vv.removeFromSuperview()
            }

        }

    }


}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}




}

还有什么我可以添加或删除来帮助解决这个问题的,欢迎提出任何建议。

更新:

所以基本上我有一个根视图控制器,它只在用户登录后显示。在用户登录之前,他们会看到另一个我正在尝试构建的视图控制器;这个视图控制器是一个注册页面,我需要在顶部有一个导航栏,并带有“登录”选项。计划是让用户点击“登录”导航控件并显示导航控制器(即in) 显示登录表单。

【问题讨论】:

  • 这个视图控制器在导航控制器中吗?

标签: swift uiviewcontroller uinavigationcontroller


【解决方案1】:

问题出在这一行:

let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: nil, action: #selector(done))

目标必须是 self 而不是 nil

let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: self, action: #selector(done))

【讨论】:

  • 同样的问题仍然存在
  • 还有其他建议吗?
  • 这个改变是绝对需要的。但这可能不是唯一的问题。
  • 我从应用委托中添加了代码。这可能是一个问题吗?
【解决方案2】:

如果您只是想在导航栏中放置一个相机按钮来推动另一个视图控制器,那么有一种更简单的方法:

func setNavigationBar() {

    let saveButton = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(done))
    navigationItem.rightBarButtonItem = saveButton
}

@objc func done() { // remove @objc for Swift 3
    let dummyViewController = UIViewController()
    navigationController?.pushViewController(dummyViewController, animated: true)
}

编辑

所以,如果我理解得很好,您正在展示一个注册页面视图控制器,而您需要一个导航按钮来导航到下一个视图,对吗?问题可能是你如何“呈现”这个视图控制器。如果您使用方法 present(_:animated:completion:),视图将以模态方式呈现,这意味着它不会有导航控制器或导航栏。

要解决这个问题,您可以创建一个 UINavigationController,将您的注册页面设置为 rootViewController,并显示导航控制器。

试试这个:

let registerViewController = RegisterViewController()
let registerNavigationController = UINavigationController(rootViewController: registerViewController)
currentViewController.present(registerNavigationController, animated: true)

【讨论】:

  • 使用我的代码,相机图标出现了,但没有推送另一个视图控制器
  • 您确定您的视图控制器在导航控制器中吗?我从适合我的代码中复制了这个 sn-p。
  • 我如何验证这一点?我最近才开始学习 Swift,如果您能解释一下,我将不胜感激
  • 你能提供更多关于你正在实现的视图控制器的上下文吗?对于刚开始使用 swift 的您,我的第一个建议是不要理会 AppDelegate,不要在其中放入任何代码,除非确实需要。您应该使用 Storyboards 来设置应用的导航流程。
  • 请看一下我的更新,我会解释我想要做什么。