为什么不打开导航控制器窗口?

时间:2018-06-25 21:34:55

标签: swift uiviewcontroller uinavigationcontroller

为什么按下相机按钮时以下代码无法打开导航控制器?完成的功能指定相机应推动视图控制器,但什么也没发生。

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:.
}




}

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

更新:

因此,基本上,我有一个根视图控制器,该视图控制器仅在用户登录后才显示。在用户登录之前,会向他们提供另一个我正在尝试构建的视图控制器;此视图控制器是一个注册页面,我需要在顶部具有一个导航栏,并带有“登录”选项。该计划是让用户点击“登录”导航控件,并向其展示导航控制器(在右侧)中显示登录表单。

2 个答案:

答案 0 :(得分:0)

问题在于此行:

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

目标必须是自己而不是

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

答案 1 :(得分:0)

如果您只是想在导航栏中放置一个摄像头按钮以按下另一个视图控制器,则有一种更简单的方法:

curve(f2(x, n = 2, m = 3), from = 0, to = 10)

编辑

因此,据我所知,您正在展示一个“注册页面”视图控制器,在那儿您需要一个导航按钮才能导航到下一个视图,对吗?问题可能出在您如何“展示”该视图控制器。如果您使用方法present(_:animated:completion :),该视图将以模态显示,这意味着它将没有导航控制器或导航栏。

要解决此问题,您可以创建一个UINavigationController,将“注册页面”设置为rootViewController,然后显示导航控制器。

尝试一下:

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