如何将视图控制器中的数据传递给另一个视图控制器中的集合视图? [复制]
Posted
技术标签:
【中文标题】如何将视图控制器中的数据传递给另一个视图控制器中的集合视图? [复制]【英文标题】:How to pass data in a View Controller to a Collection View in another View Controller? [duplicate] 【发布时间】:2017-08-11 11:18:17 【问题描述】:我正在尝试在 UITextField 中传递数据,用户将输入这些数据以进入另一个视图控制器的集合视图中的单元格。
我想在每次有人按下“上传”按钮时创建新的“帖子”以添加到我的提要中,因此“收藏视图”单元格需要以某种方式上传给定信息,然后清除该单元格。
我不知道如何将数据传递给集合视图。
这是我的视图控制器和单元格代码。
import UIKit
import Firebase
class UploadViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
@IBOutlet weak var previewImage: UIImageView!
@IBOutlet weak var postBtn: UIButton!
@IBOutlet weak var selectBtn: UIButton!
@IBOutlet weak var thisTextField: UITextField!
@IBOutlet weak var thatTextField: UITextField!
var picker = UIImagePickerController()
@IBAction func thisPhotoUpload(_ sender: Any)
if thisTextField.text != "" || thatTextField.text != ""
performSegue(withIdentifier: "segue", sender: self)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
var postCell = segue.destination as! PostCell
postCell.thisString = thisTextField.text!
postCell.thatString = thatTextField.text!
override func viewDidLoad()
super.viewDidLoad()
picker.delegate = self
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
self.previewImage.image = image
selectBtn.isHidden = true
postBtn.isHidden = false
self.dismiss(animated: true, completion: nil)
@IBAction func selectPressed(_ sender: Any)
picker.allowsEditing = true
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
@IBAction func postPressed(_ sender: Any)
AppDelegate.instance().showActivityIndicator()
let uid = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference()
let storage = FIRStorage.storage().reference(forURL: "gs://instagram-f3f20.appspot.com")
let key = ref.child("posts").childByAutoId().key
let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)
let uploadTask = imageRef.put(data!, metadata: nil) (metadata, error) in
if error != nil
print(error!.localizedDescription)
AppDelegate.instance().dismissActivityIndicatos()
return
imageRef.downloadURL(completion: (url, error) in
if let url = url
let feed = ["userID" : uid,
"pathToImage" : url.absoluteString,
"likes" : 0,
"author" : FIRAuth.auth()!.currentUser!.displayName!,
"postID" : key] as [String : Any]
let postFeed = ["\(key)" : feed]
ref.child("posts").updateChildValues(postFeed)
AppDelegate.instance().dismissActivityIndicatos()
self.dismiss(animated: true, completion: nil)
)
uploadTask.resume()
CollectionViewCell 子类
import UIKit
import Firebase
class PostCell: UICollectionViewCell
@IBOutlet weak var postImage: UIImageView!
@IBOutlet weak var authorLabel: UILabel!
@IBOutlet weak var likeLabel: UILabel!
@IBOutlet weak var likeBtn: UIButton!
@IBOutlet weak var unlikeBtn: UIButton!
@IBOutlet weak var thisLabel: UILabel!
@IBOutlet weak var thisButton: UIButton!
@IBOutlet weak var thatLabel: UILabel!
@IBOutlet weak var thatButton: UIButton!
var postID: String!
var thisString = String()
var thatString = String()
override func awakeFromNib()
super.awakeFromNib()
thisLabel.text = thisString
thisButton.setTitle(thisString, for: UIControlState.normal)
thatLabel.text = thisString
thatButton.setTitle(thisString, for: UIControlState.normal)
@IBAction func likePressed(_ sender: Any)
self.likeBtn.isEnabled = false
let ref = FIRDatabase.database().reference()
let keyToPost = ref.child("posts").childByAutoId().key
ref.child("posts").child(self.postID).observeSingleEvent(of: .value, with: (snapshot) in
if let post = snapshot.value as? [String : AnyObject]
let updateLikes: [String : Any] = ["peopleWhoLike/\(keyToPost)" : FIRAuth.auth()!.currentUser!.uid]
ref.child("posts").child(self.postID).updateChildValues(updateLikes, withCompletionBlock: (error, reff) in
if error == nil
ref.child("posts").child(self.postID).observeSingleEvent(of: .value, with: (snap) in
if let properties = snap.value as? [String : AnyObject]
if let likes = properties["peopleWhoLike"] as? [String : AnyObject]
let count = likes.count
self.likeLabel.text = "\(count) Likes"
let update = ["likes" : count]
ref.child("posts").child(self.postID).updateChildValues(update)
self.likeBtn.isHidden = true
self.unlikeBtn.isHidden = false
self.likeBtn.isEnabled = true
)
)
)
ref.removeAllObservers()
@IBAction func unlikePressed(_ sender: Any)
self.unlikeBtn.isEnabled = false
let ref = FIRDatabase.database().reference()
ref.child("posts").child(self.postID).observeSingleEvent(of: .value, with: (snapshot) in
if let properties = snapshot.value as? [String : AnyObject]
if let peopleWhoLike = properties["peopleWhoLike"] as? [String : AnyObject]
for (id,person) in peopleWhoLike
if person as? String == FIRAuth.auth()!.currentUser!.uid
ref.child("posts").child(self.postID).child("peopleWhoLike").child(id).removeValue(completionBlock: (error, reff) in
if error == nil
ref.child("posts").child(self.postID).observeSingleEvent(of: .value, with: (snap) in
if let prop = snap.value as? [String : AnyObject]
if let likes = prop["peopleWhoLike"] as? [String : AnyObject]
let count = likes.count
self.likeLabel.text = "\(count) Likes"
ref.child("posts").child(self.postID).updateChildValues(["likes" : count])
else
self.likeLabel.text = "0 Likes"
ref.child("posts").child(self.postID).updateChildValues(["likes" : 0])
)
)
self.likeBtn.isHidden = false
self.unlikeBtn.isHidden = true
self.unlikeBtn.isEnabled = true
break
)
ref.removeAllObservers()
现在,我的视图控制器中的 'var postCell = segue.destination as! PostCell'线。错误显示“执行错误”
任何和所有的帮助都会很棒。
【问题讨论】:
【参考方案1】:Segue.destination 返回 UIViewController 而不是 UICollectionViewCell
您应该在下一个视图控制器中创建一个变量并将文本分配给它,然后在那里显示它。
let vc = segue.destination as! NextViewController
vc.thisText = thisTextField.text!
vc.thatText = thatTextField.text!
然后在 NextViewController 中,您将拥有 thisText 和 thatText 中的输入
【讨论】:
我想要传递数据的标签和按钮都在单元格中,所以我无法在将数据传递给视图控制器时发送 segue 而不会出错 如果 cellForItemAt indexPath 不是这样,则将数据添加到单元格中 成功了吗?如果是的话,你能把它标记为一个好的答案吗? 它没有用 你能和我详细解释一下吗?以上是关于如何将视图控制器中的数据传递给另一个视图控制器中的集合视图? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
将 tableViewCell 中的数据传递给另一个 VC 失败
如何将计时器中的数据传递给新的 ViewController (Swift 3)