rohan
rohan

Reputation: 53

Back button not appearing in UIViewController

I have a UITableView inside my entry-point UIViewController (VC_A). I have another UIViewController (VC_B) that has details for each entry in the UITableView. I have a Show segue from VC_A to VC_B that's triggered in code when a UITableViewCell is pressed:

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)
}

(Note that CustomCell extends UITableViewCell and passedCellData is a global variable.)

My custom view controller for VC_B has a data field, so I popular that in prepareForSegue:

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

DetailController is the UIViewController for VC_B.

I don't have a UINavigationController set up in Storyboard or anything, and both UIViewController instances have UINavigationBar instances at the top (to set the title). However, there's no Back button in VC_B; its navbar only shows the title (and so once I tap for details, I can't go back).

How do I fix this?

Upvotes: 0

Views: 3283

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71852

If you drag and drop your UINavigationBar then you can drag and drop UIBarButtonItem into your UINavigationBar then you can create an action for it like shown below:

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

Upvotes: 1

Sohel L.
Sohel L.

Reputation: 9540

For that, you need to add the custom back button.

Put the below line in viewDidLoad:

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

Now, mention the selector:

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

Hope this helps!

Upvotes: 0

Related Questions