如何“等待”PopoverPresentationController 被关闭

How to "wait' for a PopoverPresentationController to be dismissed

我正在编写一个 "notebook" 风格的程序。笔记本有多个页面,我试图放入一个 "go to page" 弹出窗口以允许用户转到任何页面。弹出窗口显示每个页面缩略图的集合视图。显然,最好是:

  1. 在弹出窗口中单击所需的页面缩略图
  2. 通过委托
  3. 发送selectedPage
  4. 让接收者 mainViewController 显示 selectedPage

弹出窗口效果很好。页面 selection 工作并将 selectedPage 传回。

问题是我需要 mainViewController "wait" 供用户 select 页面。这里是 mainViewController 中的相关代码部分:

用户 select 显示弹出窗口的 barButton:

@IBAction func selectPage(sender: UIBarButtonItem) {

        self.performSegueWithIdentifier("showPages", sender: self)

    //need to wait here for the popover to be dismissed.
//the next line is executed before segue even appears
//"while" and delay don't work

    imageView.image = currentNotebook.pages[goToPage! - 1] //displays selectedPage (goToPage is set by delegation)
    goToPage = nil
}

//跳转到弹出框

case "showPages" :
            let navigationController = segue.destinationViewController as? PageCollectionViewController

                if let vc = navigationController {
                    vc.delegate = self
                    vc.modalInPopover = false
                    vc.preferredContentSize = CGSizeMake(400,100)
                    vc.notebook = self.currentNotebook
                    print("got to show pages")
        }

我怀疑 selectPage 函数中需要某种闭包或处理程序,但我想不出来。希望这已经足够清楚了。一大早的...

弹出窗口的代码如下:

import UIKit

protocol PageCollectionViewControllerDelegate {
func selectsPage(selectedPage:Int)
}

class PageCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate  {

var notebook: Notebook!
var pageNum: Int!
var delegate: PageCollectionViewControllerDelegate?

@IBOutlet weak var pageCollectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.pencilCollectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInCollectionView(pageCollectionView: UICollectionView) -> Int {    
    return 1
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return notebook.pages.count
}

func collectionView(pageCollectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = pageCollectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! PageCollectionViewCell
    let cellImage = notebook.pages[indexPath.row]
    let tempThumb = imageWithImage(cellImage, scaledToFillSize: CGSizeMake(cell.bounds.width, cell.bounds.height)) //create thumbnail of each page

    cell.pageThumb.image = tempThumb
    cell.backgroundColor = UIColor.whiteColor()

    return cell
}

func collectionView(pageCollectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    //return page number

    if let delegate =  self.delegate {
        pageNum = indexPath.row + 1
        print("page number",pageNum)

        delegate.selectsPage(pageNum)
    }
    self.dismissViewControllerAnimated(true, completion: nil) //if you comment this out the popover is  not dismissed when clicking on a cell
}


func imageWithImage(image: UIImage, scaledToFillSize size: CGSize) -> UIImage {
    let scale: CGFloat = max(size.width / image.size.width, size.height / image.size.height)
    let width: CGFloat = image.size.width * scale
    let height: CGFloat = image.size.height * scale
    let imageRect: CGRect = CGRectMake((size.width - width) / 2.0, (size.height - height) / 2.0, width, height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    image.drawInRect(imageRect)
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

}

这是一个典型的异步编程的例子。在这种情况下,您正在等待的异步事件是用户响应。

几乎普遍的答案是"don't wait. Send a message and have the event notify you when it's done."

我要做的是创建一个 UIViewController 的自定义子类作为您的弹出窗口,然后为弹出窗口提供一个完成块(闭包)属性,或者使用委托对其进行设置,然后定义一个协议,以便当用户选择一个选项时弹出窗口可以通知它的委托。 (这两种方法非常相似,但是有了完成块,您不必定义被调用的方法 - 您只需在调用弹出窗口时传入完成代码即可。)