gpt4 book ai didi

ios - 如何将 PopoverPresentationController 设置为 "wait'

转载 作者:行者123 更新时间:2023-11-28 06:49:07 26 4
gpt4 key购买 nike

我正在编写一个“笔记本”风格的程序。笔记本有多个页面,我试图放入一个“转到页面”弹出窗口以允许用户转到任何页面。弹出窗口显示每个页面缩略图的 Collection View 。显然,如果能够:

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

弹出窗口效果很好。页面选择起作用并将 selectedPage 传回。

问题是我需要让 mainViewController“等待”用户选择页面。这里是 mainViewController 中的相关代码部分:

用户选择 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
}

//跳转到popover

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
}

}

最佳答案

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

几乎普遍的回答是“不要等待。发送消息并让事件在完成时通知您。”

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

关于ios - 如何将 PopoverPresentationController 设置为 "wait',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35392178/

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