1

I turn on the editing table in the FirstViewController

@IBAction func editButtonPressed(sender: UIBarButtonItem) {
     self.tableView.allowsMultipleSelectionDuringEditing = true
     if self.editing {
          let popoverEditMenu = self.storyboard?.instantiateViewControllerWithIdentifier("popoverEditMenu") as! EditMenuTableViewController
          popoverEditMenu.modalPresentationStyle = .Popover
          popoverEditMenu.popoverPresentationController!.delegate = self
          let popover: UIPopoverPresentationController = popoverEditMenu.popoverPresentationController!
          popover.barButtonItem = sender
          presentViewController(popoverEditMenu, animated: true, completion: nil)
      } else {
          editButton.image = UIImage(named: "profile_more")
          self.editing = !self.editing
      }
}

Editing table is included successfully. After the above actions, I want to finish editing, by clicking on a table cell in a popover, code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let firstTableVC = self.storyboard?.instantiateViewControllerWithIdentifier("firstTableVC") as! FirstTableViewController

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    switch indexPath.row {
    case 0:
        self.dismissViewControllerAnimated(true, completion: nil)
        firstTableVC.editing = false // Disable Editing
        firstTableVC.editButton.image = UIImage(named: "1461294921_15.Pencil")
    default:
        break
    }
}

But there is no change in the button image, and table editing mode not disabled

  • 1
    The problem is that you are creating a completely new instance of FirstTableViewController, which is entirely separate from the original instance from which the popover is presented. There are several ways to achieve what you want, though in this instance I would recommend a delegate/protocol pattern. See "Passing data back to the previous view controller" in [this answer](http://stackoverflow.com/a/31934786). You should also bear in mind @Alexey's answer below, which is also important. – pbasdf May 15 '16 at 14:37
  • @pbasdf thank you :) – Alexey Milahin May 15 '16 at 16:09

2 Answers2

1

Solution found!

The problem was solved by the use of delegation. Thanks to @pbasdf for the tip

import UIKit

protocol SecondTableViewControllerDelegate {
    func endEditing()
}

class SecondTableViewController: UITableViewController {

    var delegate: SecondTableViewControllerDelegate?

    ...

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        switch indexPath.row {
        case 0:
            self.dismissViewControllerAnimated(true, completion: nil)
            delegate?.endEditing()
        default:
            break
        }
    }
}

Delegate function in FirstViewController. You need to specify a delegate inheritance in FirstViewController

func endEditing() {
    self.editing = false
    editButton.image = UIImage(named: "1461294921_15.Pencil")
}
  • ... but don't set `image` on a UIButton directly: use `setImage:forState:` as per @AlexeyShavitov's answer. Unless you do, the button can "forget" the image when it changes state. – pbasdf May 16 '16 at 16:50
  • @pbasdf i have navigation bar button. She does not have setImage method – Alexey Milahin May 17 '16 at 23:12
  • Apologies, I should have seen that from the context. Ignore my previous comment. – pbasdf May 18 '16 at 07:18
0

Try to use

firstTableVC.editButton.setImage(UIImage(named:"1461294921_15.Pencil"),  forState: UIControlState.Normal)

It must works.