后退按钮没有出现在UIViewController中

时间:2015-07-22 04:08:45

标签: ios iphone swift uitableview uiviewcontroller

我的入口点UITableView(VC_A)中有UIViewController。我有另一个UIViewController(VC_B),其中包含UITableView中每个条目的详细信息。我有一个从VC_A到VC_B的显示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中没有“后退”按钮;它的导航栏只显示标题(因此,一旦我点击了详细信息,我就无法返回)。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

如果您拖放UINavigationBar,则可以将UIBarButtonItem拖放到UINavigationBar,然后就可以为其创建操作,如下所示:

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

答案 1 :(得分:0)

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

将以下行放在viewDidLoad中:

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

现在,提一下选择器:

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

希望这有帮助!

相关问题