自定义后退按钮标题 "Replace" Segue
Back button title on custom "Replace" Segue
我有一个自定义的 "Replace" segue,它用另一个替换了当前的 viewcontroller。这很好用。然而,后退按钮标题总是变成 "Back"。 segue 是这样工作的:
ViewController A -> ViewController B
ViewController B 然后执行到 ViewController C 的替换 segue。然后堆栈是:
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.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
我也不想在执行 segue 之前设置后退按钮标题,因为 ViewController 包含其他可能不需要该后退按钮标题的 segue。有什么建议吗?
修复起来很容易,我只是将代码添加到 ViewController 中的 prepareForSegue:sender:
A:
public override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
switch (segue.identifier!) {
case agendaSegueId:
navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
...
}
}
我有一个自定义的 "Replace" segue,它用另一个替换了当前的 viewcontroller。这很好用。然而,后退按钮标题总是变成 "Back"。 segue 是这样工作的:
ViewController A -> ViewController B
ViewController B 然后执行到 ViewController C 的替换 segue。然后堆栈是:
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.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
我也不想在执行 segue 之前设置后退按钮标题,因为 ViewController 包含其他可能不需要该后退按钮标题的 segue。有什么建议吗?
修复起来很容易,我只是将代码添加到 ViewController 中的 prepareForSegue:sender:
A:
public override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
switch (segue.identifier!) {
case agendaSegueId:
navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
...
}
}