为什么我的主视图控制器不会更新,当我segue回来?

时间:2015-11-26 11:02:58

标签: ios xcode swift uicollectionview reloaddata

我试图创建一个非常简单的应用程序。 你在feedViewController,点击栏按钮图标(相机)。 它将您带到AddEditViewController。

在AddEditView控制器中,你想在一个数组中添加一个图片,一旦你点击“保存”,它将显示在feedViewController中 但是直到我在模拟器中停止应用并再次运行它才会显示我的图片。

import UIKit
import CoreData
import MobileCoreServices

class FeedViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
@IBOutlet weak var collectionView: UICollectionView!

var feedArray: [AnyObject] = []
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    makeItLoad()
}
override func viewDidAppear(animated: Bool) {
    self.collectionView.reloadData()
}


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

func makeItLoad(){
    let request = NSFetchRequest(entityName: "FeedItem")
    let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    let context:NSManagedObjectContext = appDelegate.managedObjectContext
    feedArray = try! context.executeFetchRequest(request)

    self.collectionView?.reloadData()
}    
@IBAction func addPhotoButtonTapped(sender: AnyObject) {
}

//UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{

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

    return feedArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell:FeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! FeedCell
    let thisItem = feedArray[indexPath.row] as! FeedItem
    cell.imageView.image = UIImage(data: thisItem.image!)
    cell.captionLabel.text = thisItem.caption
    return cell

}


}

我的addEditview是:

    import UIKit
    import CoreData
    import MobileCoreServices

    class AddEditViewController:
    UIViewController,NSFetchedResultsControllerDelegate,
    UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var item : FeedItem? = nil

@IBOutlet weak var addEditNameField: UITextField!
@IBOutlet weak var addEditTypeField: UITextField!
@IBOutlet weak var addEditHashtagField: UITextField!
@IBOutlet weak var addEditImageViewHolder: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
}


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


@IBAction func addImageFromDevice(sender: AnyObject) {
    let pickerController = UIImagePickerController()
    pickerController.delegate = self
    pickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    pickerController.allowsEditing = true

    self.presentViewController(pickerController, animated: true, completion: nil)
}

@IBAction func addImageFromCamera(sender: AnyObject) {
    let pickerController = UIImagePickerController()
    pickerController.delegate = self
    pickerController.sourceType = UIImagePickerControllerSourceType.Camera
    pickerController.allowsEditing = true

    self.presentViewController(pickerController, animated: true, completion: nil)
}

   func imagePickerController(picker: UIImagePickerController,
   didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)
    self.addEditImageViewHolder.image = image

}

@IBAction func saveButtonTapped(sender: AnyObject) {
    createNewItem()
    dismissVC()
}

@IBAction func cancelButtonTapped(sender: AnyObject) {
    dismissVC()
}
func dismissVC() {
    navigationController?.popViewControllerAnimated(true)
    FeedViewController().collectionView?.reloadData()
    FeedViewController().viewDidLoad()

}
     func createNewItem() {
     let entityDescription = NSEntityDescription.entityForName("FeedItem",
     inManagedObjectContext: FeedViewController().managedObjectContext)
     let feedItem = FeedItem(entity: entityDescription!,
     insertIntoManagedObjectContext:
     FeedViewController().managedObjectContext)

    feedItem.caption = addEditNameField.text
    feedItem.type = addEditTypeField.text
    feedItem.hashtags = addEditHashtagField.text
    feedItem.image = UIImagePNGRepresentation(addEditImageViewHolder.image!)
    FeedViewController().feedArray.append(feedItem)
    do {

        try FeedViewController().managedObjectContext.save()
    } catch {
        return
    }
}

func editItem() {
    item!.caption = addEditNameField.text
    item!.type = addEditTypeField.text
    item!.hashtags = addEditHashtagField.text
    item!.image = UIImagePNGRepresentation(addEditImageViewHolder.image!)

    do {
        try FeedViewController().managedObjectContext.save()
    } catch {
        return
    }
}

  }

所以我的问题是:一旦我按下通过segue连接到“AddEditViewController”的相机,我选择一张照片然后按“保存”。 为什么我的FeedViewController不会更新?为什么我必须停止模拟器并重新启动才能显示?

2 个答案:

答案 0 :(得分:1)

所以,在玩了一会儿并尝试学习之后,我找到了解决方案

首先,将数组添加到类之外,使其为全局数,而不是:

 import UIKit
 import CoreData
 import MobileCoreServices

 class FeedViewController: UIViewController, UICollectionViewDataSource,
 UICollectionViewDelegate, UIImagePickerControllerDelegate,
 UINavigationControllerDelegate {
 @IBOutlet weak var collectionView: UICollectionView!
 var feedArray: [AnyObject] = []

更改它,以便:

 import UIKit
 import CoreData
 import MobileCoreServices

  var feedArray: [AnyObject] = []

 class FeedViewController: UIViewController, UICollectionViewDataSource,
 UICollectionViewDelegate, UIImagePickerControllerDelegate,
 UINavigationControllerDelegate {
 @IBOutlet weak var collectionView: UICollectionView!

而不是使用

    FeedViewController().feedArray.append(feedItem)

现在你可以使用:

    feedArray.append(feedItem)

问题是,每次我使用的时候:“FeedViewController()。费用......” 我创建了一个新实例,所以我实际上没有正确附加数组。

我希望它有所帮助。

答案 1 :(得分:0)

在我看来,当您将新图片保存到数据库服务,并且确实将其添加到用于填充集合视图的数组时,您永远不会重新加载集合视图。出于某种原因,如果用户取消拍摄新照片,则会重新加载,这似乎是多余的,但在拍摄新照片后不会重新加载,这似乎很重要。

注意:有更多有效的方法可以将新数据添加到collectionView而不是.reloadData().reloadData()将重新加载所有单元格,而方法.insertItemsAtIndexPaths()将只添加1个新单元格。