ในแอป iOS 8 ของฉัน ฉันมี ViewController แบบกำหนดเองซึ่งฉันนำเสนอเป็น Popover ViewController นี้มีผู้รับมอบสิทธิ์ซึ่งรับและส่งไปยัง ViewController หลักซึ่งเป็นดัชนีที่ถูกคลิกในป๊อปอัป ปัญหาคือฉันไม่สามารถยกเลิก Popover นี้หลังจาก selectRow ได้
นี่คือรหัส:
นี่คือวิธีการที่ฉันเรียกเมื่อฉันต้องการแสดงป๊อปอัป
@IBAction func registerButtonAction(sender: UIButton) {
popup = self.storyboard!.instantiateViewControllerWithIdentifier("PopupViewController") as? PopupViewController
popup!.modalPresentationStyle = .Popover
popup!.preferredContentSize = CGSizeMake(100, 120)
let popoverMenuViewController = popup!.popoverPresentationController
popoverMenuViewController?.permittedArrowDirections = .Up
popoverMenuViewController?.delegate = self
popoverMenuViewController?.sourceView = sender
popoverMenuViewController?.sourceRect = CGRect(
x: sender.frame.size.width/2,
y: sender.frame.size.height/2,
width: 1,
height: 1)
popup!.delegate = self
presentViewController(
popup!,
animated: true,
completion: nil)
}
นี่คือรหัส PopupViewController:
protocol PopupViewControllerDelegate
{
func rowClickedAtIndex(var index : Int)
}
class PopupViewController: MainViewController {
var delegate : PopupViewControllerDelegate?
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension PopupViewController:UITableViewDelegate{
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:PopupTableViewCell? = tableView.dequeueReusableCellWithIdentifier("PopupTableViewCell") as? PopupTableViewCell
if cell == nil {
cell = PopupTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "PopupTableViewCell")
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (self.delegate != nil)
{
self.delegate!.rowClickedAtIndex(indexPath.row)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return 2
}
}
ขอบคุณ