为啥当我返回时我的主视图控制器不会更新?
Posted
技术标签:
【中文标题】为啥当我返回时我的主视图控制器不会更新?【英文标题】:why won't my main viewcontroller update when i segue back?为什么当我返回时我的主视图控制器不会更新? 【发布时间】:2015-11-26 11:02:57 【问题描述】:我正在尝试创建一个非常简单的应用程序。 您在 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 不会更新?为什么我必须停止模拟器并重新启动它才能显示?
【问题讨论】:
【参考方案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().fee...” 我创建了一个新实例,所以实际上我没有正确附加数组。
希望对你有所帮助。
【讨论】:
这也是另一个问题的答案:“从不同的类快速追加到数组”。所以要提出一个重要的问题是,我无法将来自不同类的数组附加到我的数组中,那是因为我的数组不是全局的【参考方案2】:在我看来,虽然您确实将新图片保存到数据库服务,并且确实将其添加到用于填充集合视图的数组中,但您永远不会重新加载集合视图。出于某种原因,如果用户取消拍摄新照片,您会重新加载它,这似乎是多余的,但在拍摄新照片后不要重新加载,这似乎很重要。
注意:向 collectionView 添加新数据的方法比.reloadData()
更有效。 .reloadData()
将重新加载所有单元格,而 .insertItemsAtIndexPaths()
方法只会添加 1 个新单元格。
【讨论】:
感谢@Acoop 的帮助它没有用。在我的 SaveButton 操作中,我使用了使用 .reloadData() 的“dismissVC”。以上是关于为啥当我返回时我的主视图控制器不会更新?的主要内容,如果未能解决你的问题,请参考以下文章