嗨,我有一个弹出窗口视图,显示一个数组。我想知道是否有一种方法,我可以以某种方式分离回哪个项目在数组中被选中。
@IBAction func popOverButton(_ sender: UIButton)
{
let controller = TableViewController()
//This is just a regular tableViewController nothing special
controller.modalPresentationStyle = .popover
// configure the Popover presentation controller
let popController: UIPopoverPresentationController? = controller.popoverPresentationController
popController?.permittedArrowDirections = [.down]
popController?.delegate = self
popController?.sourceView = sender
popController?.sourceRect = sender.bounds
popController?.backgroundColor = .white
self.parent?.present(controller, animated: true, completion: { })
}
Here is what it looks like
谢谢你的帮助
最佳答案
最简单的方法是创建一个委托,当选择一个单元格时,将选择传递回呈现视图控制器。然后设置UITableViewDelegate方法来调用delegate方法。像这样的:
@protocol PopoverOptionSelectionDelegate {
func itemSelected(item:String);
}
在演示VC中实现该方法
class PresnetingViewController, PopoverOptionSelectionDelegate {
@IBAction func popOverButton(_ sender: UIButton) {
let controller = TableViewController()
controller.delegate = self //----Important---//
//This is just a regular tableViewController nothing special
controller.modalPresentationStyle = .popover
// configure the Popover presentation controller
let popController: UIPopoverPresentationController? =
controller.popoverPresentationController
popController?.permittedArrowDirections = [.down]
popController?.delegate = self
popController?.sourceView = sender
popController?.sourceRect = sender.bounds
popController?.backgroundColor = .white
self.parent?.present(controller, animated: true, completion: { })
}
func itemSelected(item:String) {
//DISMISS YOUR POPOVER MAYBE AND DO SOMETHING WITH "ITEM" HERE
}
}
class TableViewController,UITableViewDelegate {
weak var delegate:PopoverOptionSelectionDelegate?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
delegate?.itemSelected(self.itemsArray[indexPath.row])
}
}
关于swift - popoverController不隔离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42984769/