gpt4 book ai didi

swift - popoverController 没有 seguing

转载 作者:行者123 更新时间:2023-11-28 15:46:22 27 4
gpt4 key购买 nike

您好,我有一个显示数组的弹出 View 。我想知道是否有一种方法可以让我以某种方式返回选择了数组中的哪个项目。

 @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

感谢任何帮助

最佳答案

最简单的方法是创建一个委托(delegate),当一个单元格被选中时,将选择传递回呈现 View Controller 。然后设置 UITableViewDelegate 方法 didSelectRowAtIndexPath 以调用委托(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 没有 seguing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42984769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com