后退按钮未出现在 UIViewController 中

Back button not appearing in UIViewController

我的 entry-point UIViewController (VC_A) 里面有一个 UITableView。我还有另一个 UIViewController (VC_B),其中包含 UITableView 中每个条目的详细信息。我有一个从 VC_A 到 VC_B 的 Show segue,它在按下 UITableViewCell 时在代码中触发:

var passedCellData: CellData!
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("it worked!")
    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell
    passedCellData = currentCell.data
    self.performSegueWithIdentifier("OpenDetailSegue", sender: self as UIViewController)
}

(注意 CustomCell 扩展了 UITableViewCellpassedCellData 是一个全局变量。)

我的 VC_B 自定义视图控制器有一个 data 字段,所以我在 prepareForSegue 中流行它:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "OpenDetailSegue") {
        let svc = segue.destinationViewController as! DetailController
        svc.data = passedCellData
    }
}

DetailController 是 VC_B 的 UIViewController

我没有在 Storyboard 或任何东西中设置 UINavigationController,并且两个 UIViewController 实例在顶部都有 UINavigationBar 个实例(用于设置标题)。但是,VC_B 中没有后退按钮;它的导航栏只显示标题(所以一旦我点击查看详细信息,我就不能返回)。

我该如何解决这个问题?

为此,您需要添加自定义后退按钮。

将以下行放入 viewDidLoad:

self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back.png"), style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")

现在,提到选择器:

func goBack(){
        self.navigationController?.popToRootViewControllerAnimated(true)
    }

希望对您有所帮助!

如果你拖放 UINavigationBar 然后你可以拖放 UIBarButtonItem 到你的 UINavigationBar 然后你可以为它创建一个动作,如下所示:

@IBAction func backButton(sender: AnyObject) {
    //dismiss your viewController 
    self.dismissViewControllerAnimated(true, completion: nil)
}