2

I've been trying to figure out how to use the UINavigationBarDelegate method shouldPopItem with a popover in Swift. I've done lots o' digging around and trying this and that, with no success. I'm hopeful someone can point me in the right direction.

I start with a UIViewController that has a number of buttons the user can click. As an example, there is a button that calls back to this method:

@IBAction func manageVerbListsButtonPressed(sender: UIButton) {
    vc = ManageListsViewController(nibName: "ManageListsView", bundle: nil)
    vc.preferredContentSize = CGSizeMake(600, 600)
    vc.modalPresentationStyle = .Popover
    let popoverController = vc.popoverPresentationController!
    popoverController.sourceView = sender
    popoverController.sourceRect = sender.bounds
    popoverController.permittedArrowDirections = .Left
    popoverController.delegate = self  // could you set the popover delegate to the vc, so the vc could control the dismiss?
    presentViewController(vc, animated: true, completion: nil)

}

This correctly opens a popover with ManageListsView as the presenting controller. That controller has a view with a table. When the user clicks on a table row, the didSelectRowAtIndexPath method fires:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let vc = EditOneListViewController()
    let vl = Lists[indexPath.row]   // this is an array of list names
    vl.isNew = false
    vc.List = vl   // give the next controller the list to edit
    navigationController?.pushViewController(vc, animated: true)
}

Again, this correctly pushes the EditOneListViewController.

What I want to do, but haven't figured out how, is to use the UINavigationBarDelegate method shouldPopItem to determine whether the navController should pop back to the table view, depending on whether the user has done something on the EditOneListViewController view. That is, if the user has edited something and not saved it, I want to use the shouldPopItem to put up an Alert indicating that and return to the view so the user can save the edits. (The list has an isDirty boolean that I can test for whether it's been saved.) If the user has saved edits, I want the navController to pop back a level.

I have done this in Obj C in earlier iOS's, but I'm darned if I can figure out how to do it with iOS 9 and Swift and presentationControllers. Any help at all will be greatly appreciated.

johnz
  • 489
  • 2
  • 17
  • Have a look at this post: http://stackoverflow.com/questions/1214965/setting-action-for-back-button-in-navigation-controller – gone Mar 20 '16 at 17:02

0 Answers0