ยกเลิก Popover ViewController จาก Tableviewcell ใน Swift

ในแอป 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
    }
}

ขอบคุณ


person Bellots    schedule 15.05.2015    source แหล่งที่มา


คำตอบ (2)


ฉันแก้ไขด้วยตัวเอง:

ฉันตรวจสอบว่าปัญหาไม่ใช่ว่าป๊อปโอเวอร์ไม่ได้ถูกไล่ออก แต่มันถูกไล่ออก แต่หลังจากนั้นไม่กี่วินาที

ดังนั้นฉันจึงวางสายที่ยกเลิกไว้ในเธรดหลัก และมันก็ทำงานได้อย่างสมบูรณ์แบบ นี่คือรหัส:

extension WelcomeViewController: PopupViewControllerDelegate {
    func rowClickedAtIndex(index: Int) {
        dispatch_async(dispatch_get_main_queue(),{
            self.dismissViewControllerAnimated(true, completion: nil)
            println(index)
        })
    }
}

ฉันอยากจะขอบคุณแฟรงกี้ที่ช่วยฉันค้นหาวิธีแก้ไข และขจัดสิ่งที่ไม่ควรเป็นปัญหาออกไป

person Bellots    schedule 15.05.2015

ย้ายการโทร dismissViewControllerAnimated(true, completion: nil) ของคุณไปยังผู้รับมอบสิทธิ์ที่ส่วนท้ายของเมธอด clickedRowAtIndex กล่าวอีกนัยหนึ่ง ตัวควบคุมมุมมองการนำเสนอควรเรียกการยกเลิก ไม่ใช่ตัวควบคุมมุมมองที่นำเสนอ

ลองสิ่งนี้:

extension WelcomeViewController: PopupViewControllerDelegate { 
    func rowClickedAtIndex(index: Int) {      
        dismissViewControllerAnimated(true, completion: nil)   
        println(index) 
    } 
}
person Frankie    schedule 15.05.2015
comment
ฉันได้ลองที่นี่แล้ว: extension WelcomeViewController: PopupViewControllerDelegate { func rowClickedAtIndex(index: Int) { popup?.dismissViewControllerAnimated(true, allowance: nil) println(index) } } แต่มันใช้งานไม่ได้.. มันผ่านในวิธีนี้เพราะ มันพิมพ์ดัชนี แต่จะไม่ยกเลิก - person Bellots; 15.05.2015
comment
ดูเหมือนว่า popup ยังคงพยายามทำการโทรอยู่ WelcomeViewController จำเป็นต้องดำเนินการดังกล่าว dismissViewControllerAnimated(true, completion: nil) - person Frankie; 15.05.2015
comment
ฉันควรทำอย่างไรในความคิดเห็นของคุณ? - person Bellots; 15.05.2015
comment
มันแปลกจริงๆ: ฉันสังเกตเห็นว่าถ้าฉันแตะแถวทันทีที่มีการนำเสนอ แถวนั้นจะทำงานได้อย่างถูกต้อง หากฉันรอสักครู่หรือนานกว่านั้น ฉันจะต้องแตะแถวสองครั้งเพื่อปิด - person Bellots; 15.05.2015
comment
rowClickedAtIndex จะถูกเรียกเพียงครั้งเดียวหรือสองครั้งเมื่อคุณต้องแตะแถวสองครั้งหรือไม่ - person Frankie; 16.05.2015
comment
วิธีการนี้ถูกเรียกสองครั้ง อันที่จริง println ของฉันถูกพิมพ์สองครั้งบนคอนโซลของฉัน - person Bellots; 16.05.2015
comment
ให้เราสนทนาต่อในการแชท - person Bellots; 16.05.2015