从Swift中的Tableviewcell中解除Popover ViewController

时间:2015-05-15 20:23:58

标签: ios iphone swift uipopovercontroller

在我的iOS 8应用程序中,我有一个自定义的ViewController,我将其作为Popover呈现。这个ViewController有一个委托,它在弹出窗口中获取并发送给父ViewController,点击索引。问题是我不能在selectRow之后解雇这个Popover。

以下是代码:

这是我想要显示弹出窗口时调用的方法。

 @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
    }
}

谢谢。

2 个答案:

答案 0 :(得分:4)

我自己解决了:

我检查过问题并不是说popover没有被解雇,但是在不同的秒数之后被解雇了。

所以我把我的dismiss调用放在主线程中并且它完美地运行了。这是代码:

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

我要感谢Frankie,他帮我找到了解决方案,删除了不应该出现的问题。

答案 1 :(得分:2)

dismissViewControllerAnimated(true, completion: nil)来电转移到clickedRowAtIndex方法末尾的委托中。换句话说,呈现视图控制器应调用解雇,而不是呈现的视图控制器。

试试这个:

extension WelcomeViewController: PopupViewControllerDelegate { 
    func rowClickedAtIndex(index: Int) {      
        dismissViewControllerAnimated(true, completion: nil)   
        println(index) 
    } 
}
相关问题