自定义后退按钮标题"替换" Segue公司

时间:2016-03-18 08:17:15

标签: ios uiviewcontroller uinavigationcontroller segue uinavigationitem

我有一个习惯"替换" segue用另一个替换当前的viewcontroller。这很好用。但是后退按钮标题总是变为"返回"。 segue的工作原理如下:

ViewController A - > ViewController B

ViewController B然后执行替换segue到ViewController C.然后堆栈:

ViewController A - > ViewController C

这是ReplaceSegue.swift的代码:

public class ReplaceSegue: UIStoryboardSegue
{
    public var animated = true

    public override func perform()
    {
        let navigationController: UINavigationController = sourceViewController.navigationController!

        var controllerStack = navigationController.viewControllers
        let index = controllerStack.indexOf(sourceViewController)
        controllerStack[index!] = destinationViewController

        navigationController.setViewControllers(controllerStack, animated: animated)
    }
}

对于我的ViewController:

func replaceAgendaViewController()
{
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let agendaViewController = mainStoryboard.instantiateViewControllerWithIdentifier("AgendaViewController") as! AgendaViewController
    let segue = ReplaceSegue(identifier: replaceSegueId, source: self, destination: agendaViewController) { () -> Void in

    }
    segue.animated = false
    segue.perform()
}

尝试过这段代码,但我理解为什么它不起作用,因为它在被替换的ViewController上设置了navigationItem:

navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil) 

我也不想在执行segue之前设置后退按钮标题,因为ViewController包含可能不需要后退按钮标题的其他segue。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

修复非常简单,我刚刚将代码添加到ViewController A中的prepareForSegue:sender:

public override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    switch (segue.identifier!) {
    case agendaSegueId:
        navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil) 
        ...
    }
}