如何禁用在 Popover 中编辑 UITableViewController
How to disable editing UITableViewController in Popover
我在 FirstViewController
中打开编辑 table
@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
}
}
编辑 table 已成功包含。完成上述操作后,我想通过单击弹出窗口中的 table 单元格来完成编辑,代码:
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
}
}
但是按钮图片没有变化,table编辑模式没有被禁用
尝试使用
firstTableVC.editButton.setImage(UIImage(named:"1461294921_15.Pencil"), forState: UIControlState.Normal)
一定有用。
已找到解决方案!
问题已通过使用委托解决。感谢@pbasdf 的提示
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
}
}
}
FirstViewController 中的委托函数。需要在FirstViewController
中指定委托继承
func endEditing() {
self.editing = false
editButton.image = UIImage(named: "1461294921_15.Pencil")
}
我在 FirstViewController
中打开编辑 table@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
}
}
编辑 table 已成功包含。完成上述操作后,我想通过单击弹出窗口中的 table 单元格来完成编辑,代码:
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
}
}
但是按钮图片没有变化,table编辑模式没有被禁用
尝试使用
firstTableVC.editButton.setImage(UIImage(named:"1461294921_15.Pencil"), forState: UIControlState.Normal)
一定有用。
已找到解决方案!
问题已通过使用委托解决。感谢@pbasdf 的提示
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
}
}
}
FirstViewController 中的委托函数。需要在FirstViewController
中指定委托继承func endEditing() {
self.editing = false
editButton.image = UIImage(named: "1461294921_15.Pencil")
}