简体   繁体   English

在导航控制器中设置后退按钮

[英]Setting Back Button in Navigation Controller

So I have 3 View controllers : V1, V2 and V3. 所以我有3个View控制器:V1,V2和V3。 When I press a button on V1 it shoots me over to V3. 当我按下V1上的按钮时,它会将我射向V3。 I would like to keep the "Back Arrow" but change the text there. 我想保留“后退箭头”,但在那里更改文本。 I have tried many things but the word back either reapers when the view is shown or my code does not work. 我尝试了很多事情,但是显示视图或我的代码不起作用时,单词back会增加。

Any Suggestions? 有什么建议么?

I already used this code 我已经用过这段代码

self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "YO", style: .Plain, target: nil, action: nil)

let backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Image can be downloaded from here below link
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)

func backAction() -> Void {
    self.navigationController?.popViewControllerAnimated(true)
}

I tried creating my Button with this code I found on Stack But the button disappeared and the back button appeared in its place again. 我尝试使用在堆栈上找到的这段代码创建Button,但是该按钮消失了,并且后退按钮再次出现在其位置。

You can set navigationItem.leftBarButtonItem to a button of your own design. 您可以将navigationItem.leftBarButtonItem设置为您自己设计的按钮。 That overrides backBarButtonItem , and iOS will not replace your text with "Back". backBarButtonItem覆盖backBarButtonItem ,iOS不会将文本替换为“ Back”。 But if you want an arrow you would have to design it yourself as part of your button. 但是,如果您想使用箭头,则必须自己将其设计为按钮的一部分。

In my code I set the button whenever the title is changed: 在我的代码中,只要更改标题,就设置按钮:

override var title:String? {
    get {
      return super.title
    }
    set {
      super.title = newValue
      if (navigationController?.childViewControllers.count > 1) {
        let backButton = // ... custom button
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
        backButton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
      } else {
        navigationItem.hidesBackButton = true
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM