ios - 添加照片,并重新加载嵌入式照片容器 View Controller

标签 ios swift uistoryboardsegue uicontainerview

我有一个 View Controller ,它收集信息,包括将多张照片添加到记录的能力。添加每张照片时,新照片将显示为特色照片(VC 中的 Storyboard UIImageview)。每张照片还应添加到图像数组中,然后将其传递给具有自己的自定义 View Controller 的容器 View 并显示。添加每张照片时,都会显示特色照片,但容器不会重新加载图像。

当现有照片数组通过 prepare for segue 方法传入时,照片容器确实可以正常工作,如下所示:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if (segue.identifier == "segueToPhotoCollection")
    {
        if let imagesArray = images
        {
            print("photos prepared")
            let controller =  (segue.destinationViewController as! PhotoCollectionVC)
            controller.images = imagesArray
            controller.delegate = self
        }
    }

如果没有照片,容器仍然加载(并打印“容器已加载,但没有照片”)。

class PhotoCollectionVC: UICollectionViewController
{
var images: [UIImage]?

weak var delegate: FeatureImageController?

override func viewDidLoad()
{
    super.viewDidLoad()

    if images?.count > 0
    { print("container loaded with photos: \(images!.count)") }
    else { print ("container loaded, but no photos") }

我需要的是以某种方式调用 prepareForSegue 方法或找到另一种方法来在添加图像后更新和重新加载容器。选择器 didFinishPickingMediaWithInfo 看起来像:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    picker.dismissViewControllerAnimated(true, completion:
    {
        var imageTaken = info[UIImagePickerControllerEditedImage] as? UIImage
        ...

        if let imageTakenNotNil = imageTaken
        {
            if self.images == nil
            { self.images = [] }
            self.images?.append(imageTakenNotNil)

然后一行会以某种方式将该图像数组重新传递给容器。我需要委托(delegate)方法来执行此操作吗?!?

最佳答案

在您的主视图 Controller 中,添加一个变量来维护对嵌入式 View Controller 的引用。在 prepareForSegue 中,分配此引用。然后在 didFinishPickingMediaWithInfo 中,您可以使用此引用在嵌入式 View Controller 中进行更改并刷新它。

因此,在您的 IBOutlets 下,您将拥有类似以下内容的内容:

var photoControlView: PhotoCollectionVC?

你的 prepareForSegue 会是这样的:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if (segue.identifier == "segueToPhotoCollection")
    {
        if let imagesArray = images
        {
            print("photos prepared")
            let controller =  (segue.destinationViewController as! PhotoCollectionVC)
            controller.images = imagesArray
            controller.delegate = self
            self.photoControlView = controller
        }
    }

在 didFinishPickingMediaWithInfo 中,您现在可以:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    picker.dismissViewControllerAnimated(true, completion:
    {
        var imageTaken = info[UIImagePickerControllerEditedImage] as? UIImage
        ...

        if let imageTakenNotNil = imageTaken
        {
            if self.images == nil
            { self.images = [] }
            self.images?.append(imageTakenNotNil)
            self.photoControlView!.images = self.images
            self.photoControlView!.reloadData()
            self.photoControlView!.setNeedsDisplay()

我不确定 setNeedsDisplay() 调用对于刷新 View 是否必要,甚至是否正确,但我之前使用过此方法的其余部分并且知道它有效。

关于ios - 添加照片,并重新加载嵌入式照片容器 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37678903/

相关文章:

ios - 如何从 ARKit 相机获取外部矩阵?

swift - 对于 iOS 10,每周特定的一天重复 UserNotification

ios - 如何创建非动画展开自定义转场

swift - 准备Segue : one VC to another VC with optionals?

iphone - 使用 xcode 4.2 调试代码时出现问题

ios - UIAlertcontroller 中的可点击 url

环球银行金融电信协会2 : multiline MKPointAnnotation

objective-c - 在 Xcode Storyboard 中使用 Unwind Segue 关闭 Popover

ios - 如何在 iOS 中以编程方式更改 UIButton 中的 textLabel 属性?

ios - 如何读取 info.plist 中的环境变量?