以编程方式创建的后退按钮不会显示在导航栏中?

时间:2017-02-18 03:17:36

标签: ios iphone swift swift3

为了简化这一点,由于应用程序设计我不能使用嵌入式导航控制器,而必须使用视图控制器上的手动后退按钮移回上一个。据我所知,以前的视图控制器也以编程方式推送到这一个。所以我在这里找到了许多不同问题的按钮的优秀代码,我为这个应用程序编写了它。问题是,当我运行程序时,我创建的条形按钮项不会显示。代码显示没有错误,我很困惑为什么没有出现。我将包含图像和代码,以使其尽可能清晰。一如既往地感谢您提供的任何帮助!!!

使用的代码:

@IBOutlet weak var roomImage: UIImageView!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var roomName: UINavigationItem!

override func viewDidLoad() {
    super.viewDidLoad()

    // Back Button Code

    let backButton = UIBarButtonItem(title:"Back",style: UIBarButtonItemStyle.plain, target: self, action: #selector(DetailView.back(sender:)))
    self.navigationItem.leftBarButtonItem = backButton

    navigationItem.rightBarButtonItem = backButton
    navigationItem.hidesBackButton = false

    // Hide da tab bar

    self.tabBarController?.tabBar.isHidden = true

    // Do any additional setup after loading the view.


    // Enter data from other view controllers

    //roomName.title = stringPassed
    roomImage.image = imagePassed
    locationLabel.text = locationPassed

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

// Back Button Action

func back(sender: UIBarButtonItem) {

    if let navigationController = self.navigationController {

        navigationController.popViewController(animated: true)

    }
}

}

带导航栏的实际故事板

运行应用时

难道我没有做出某种出口或类似的东西吗?谢谢!!

故事板布局

详细信息视图的树模型(带有导航栏问题的树模型)

编辑:仍然没有工作,但你给出的链接和想法给了我很多新的想法和选择,直到一个更具体的答案出现。谢谢!!!

2 个答案:

答案 0 :(得分:1)

你能说明你是如何推动DetialView控制器的 我以编程方式累了 像这样

func showDetialViewController(){
let detialViewController = UIViewConroller()
navigationController?.pushViewController(detialViewController, animated: true)
} 

答案 1 :(得分:1)

将您从“控件库”面板拖动的NavigationBar移至ViewController。当您使用navigationItem设置self.navigationItem时,它不会引用该navigationBar项,因为它与之无关。

当您使用ViewController推送navigationController?.pushViewController(myVC, animated: true)时,它会自动在NavigationBar屏幕上显示myVC