I have code below in a collection view and its result, what I want to know is how to access those data from my collection view to my function prepare. I want to get the value of getTempDetails like
getTempDetails["name"] as! String
to be the value of my
destination.chore_name = getTempDetails["name"] as! String
but I can't seem to access it. thanks
code for prepare
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let distanation = segue.destination as? ChoreDetailsViewController {
distanation.chore_name = "Hi"
}
}
code for
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
// print("You selected cell #\(indexPath.item)!")
// print("emong gepili:" , titleArray[indexPath.row])
if indexPath.row == 0 {
performSegue(withIdentifier: "goToSegue", sender: nil)
} else {
performSegue(withIdentifier: "gotoDetails", sender: nil)
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
print("You selected ID #\( getTempDetails["reward"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["desc"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["sched"] as? String ?? "" )!")
Possibly you can use:
var strReward:String = String()
var strDesc:String = String()
var sreSched:String = String()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
performSegue(withIdentifier: "goToSegue", sender: nil)
} else {
performSegue(withIdentifier: "gotoDetails", sender: nil)
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
strReward = (getTempDetails["reward"] as? String)!
strDesc = (getTempDetails["desc"] as? String)!
sreSched = (getTempDetails["sched"] as? String)!
}
}
}
Pass this Data to next VC or can use them in any way you want
Note:- I am not sure what data type is the data coming, I have assumed it to be a String type. You can use accordingly
use the keyword of indexPathsForSelectedItems for colletionview
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "gotoDetails" ,
let nextScene = segue.destination as? ChoreDetailsViewController ,
let indexPath = self.yourCollectionViewName. indexPathsForSelectedItems().first {
if let getTempDetails: [String : Any] = getAllDetail[indexPath.item] {
print("You selected ID #\( getTempDetails["reward"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["desc"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["sched"] as? String ?? "" )!")
nextScene.chore_name = getTempDetails["name"] as! String
}
}
}
You can store last selected items data in class var
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
performSegue(withIdentifier: "goToSegue", sender: nil)
} else {
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row],
let name = getTempDetails["name"] as? String {
selectedChoreName = name
performSegue(withIdentifier: "gotoDetails", sender: nil)
}
and then
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let distanation = segue.destination as? ChoreDetailsViewController {
distanation.chore_name = selectedChoreName
selectedChoreName = "" //optional
}
}
Related
While using segment control to populate the respective list under different segments, issues I am facing are as below
When the segment control is loaded in the tableview , the first segment is working fine and when I switch second segment it do populate the list but it gives error of Index Out Of Range when the list scrolled
The second segment control should load the different list as per the filter, but it loads the same list as the first segment control
I am using Swift IOS and Firestore Database for this
Below is the code I am sharing
class FirstSegementViewController: UIViewController {
#IBOutlet var segmentControl:UISegmentedControl!
#IBOutlet var tableView: UITableView!
var firstDetails:[FirstDetails] = []
var secondDetails:[SecondDetails] = []
var postKey:String = ""
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view.
retrieveAllPosts()
}
func retrieveAllPosts(){
let postsRef = Firestore.firestore().collection("posts").whereField("post_author_id", isEqualTo: Auth.auth().currentUser!.uid
).whereField("status", isEqualTo: false).limit(to: 50)
postsRef.getDocuments { (snapshot, error) in
if let error = error {
print(error.localizedDescription)
} else {
if let snapshot = snapshot {
for document in snapshot.documents {
let data = document.data()
//self.postKey = document.documentID
let username = data["post_author_username"] as? String ?? ""
let postTitle = data["postTitle"] as? String ?? ""
let postcategory = data["postcategory"] as? String ?? ""
let postContent = data["postContent"] as? String ?? ""
let postAuthorProfilePicUrl = data["post_user_profile_pic_url"] as? String ?? ""
let postAuthorSpinnerC = data["post_author_spinnerC"] as? String
let newSourse = FirstDetails(_documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent, _postuserprofileImagUrl: postAuthorProfilePicUrl, _postAuthorSpinncerC: postAuthorSpinnerC)
self.firstDetails.append(newSourse)
// print(self.postKey)
}
self.tableView.reloadData()
}
}
}
}
func retrieveAllPosts2(){
let postsRef = Firestore.firestore().collection("posts").whereField("post_author_id", isEqualTo: Auth.auth().currentUser!.uid).whereField("status", isEqualTo: true).limit(to: 50)
postsRef.getDocuments { (snapshot, error) in
if let error = error {
print(error.localizedDescription)
} else {
if let snapshot = snapshot {
for document in snapshot.documents {
let data = document.data()
//self.postKey = document.documentID
let username = data["post_author_username"] as? String ?? ""
let postTitle = data["postTitle"] as? String ?? ""
let postcategory = data["postcategory"] as? String ?? ""
let postContent = data["postContent"] as? String ?? ""
let postAuthorProfilePicUrl = data["post_user_profile_pic_url"] as? String ?? ""
let postAuthorSpinnerC = data["post_author_spinnerC"] as? String
let newSourse1 = SecondDetails(_documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent, _postuserprofileImagUrl: postAuthorProfilePicUrl, _postAuthorSpinncerC: postAuthorSpinnerC)
self.secondDetails.append(newSourse1)
}
self.tableView.reloadData()
}
}
}
}
#IBAction func indexChanged(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
retrieveAllPosts()
// label1.text = "First Segment Selected"
case 1:
retrieveAllPosts2()
// label1.text = "Second Segment Selected"
default:
break
}
//self.tableView.reloadData()
}
#objc func toComments(_ sender: AnyObject) {
let commentbutton = sender as! UIButton
let post = firstDetails[commentbutton.tag]
postKey = post._documentId // or what key value it is
print("hello")
performSegue(withIdentifier: "toCommentsListforMWs", sender: self)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
var vc = segue.destination as! CommentListViewController
vc.postId = postKey
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension FirstSegementViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var value = 0
switch segmentControl.selectedSegmentIndex{
case 0:
value = firstDetails.count
break
case 1:
value = secondDetails.count
break
default:
break
}
return value
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyWsPostCell
switch segmentControl.selectedSegmentIndex{
case 0:
cell.firstdetails1 = firstDetails[indexPath.row]
cell.commentbutton.tag = indexPath.row
cell.commentbutton.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)
break
case 1:
cell.completed1 = secondDetails[indexPath.row]
cell.commentbutton.tag = indexPath.row
cell.commentbutton.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)
break
default:
break
}
return cell
}
}
Change your indexChanged(_ sender: UISegmentedControl) function like this:
#IBAction func indexChanged(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
self.firstDetails.removeAll()
retrieveAllPosts()
// label1.text = "First Segment Selected"
case 1:
self.secondDetails.removeAll()
retrieveAllPosts2()
// label1.text = "Second Segment Selected"
default:
break
}
//self.tableView.reloadData()
}
In your retrieveAllPosts() and retrieveAllPosts2() you need to empty the data you have already stored in the firstDetails and secondDetails array. Currently you are just appending the data everytime user changes the segment index.
func retrieveAllPosts() {
self.firstDetails = []
//all of your code here
}
func retrieveAllPosts2() {
self.secondDetails = []
//all of your code here
}
Before your Api hit, or before adding data to array remove the previous data in the array.
#IBAction func indexChanged(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
self.firstDetails.removeAll()// Remove data in array before u call Api
retrieveAllPosts()
case 1:
self.secondDetails.removeAll()// Remove data in array before u call Api
retrieveAllPosts2()
default:
break
}
}
I am going to try to be as clear as I can be on this question but if you need more information please please ask. I have a tableviewcontroller that has a list of all the messages the logged in user has had with other users of the app. When clicked the logged in user clicks a cell, I would like for the user to segue to a view controller that allows them to chat with whatever user they like. This chat was acquired using JSQMessageController. However, when I set it the segue in the tableviewcontroller, show below:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let message = messages[indexPath.row]
if message.ReceiverId != self.loggedInUserUid {
var newVariable = message.ReceiverId
if self.userpicuid == newVariable {
let ref = FIRDatabase.database().reference().child("users").child(userpicuid!)
ref.observeSingleEvent(of: .value, with: { (snapshot)
in
if let dictionary = snapshot.value as? [String: AnyObject]{
for post in dictionary {
let messages = post.value as! [String: AnyObject]
for (id, value) in messages {
self.username = messages["username"] as? String
}}}})}} else if message.senderId != self.loggedInUserUid {
let newVariable = message.senderId
if self.userpicuid == newVariable {
let ref = FIRDatabase.database().reference().child("users").child(userpicuid!)
ref.observeSingleEvent(of: .value, with: { (snapshot)
in
if let dictionary = snapshot.value as? [String: AnyObject]{
for post in dictionary {
let messages = post.value as! [String: AnyObject]
for (id, value) in messages {
self.username = messages["username"] as? String
}}}})}
}
performSegue(withIdentifier: "MessageNow", sender: self.userpicuid)
}
override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "MessageNow", let chatVc = segue.destination as? SendMessageViewController else {
return
}
chatVc.senderId = self.loggedInUser?.uid
chatVc.receiverData = sender as AnyObject
chatVc.senderDisplayName = self.userpicuid
chatVc.username = self.username
}
I get an error in the MessageViewController:
var receiverData: AnyObject?
override func viewDidLoad() {
super.viewDidLoad()
let receiverId = receiverData as! String
let receiverIdFive = String(receiverId.characters.prefix(5))
let senderIdFive = String(senderId.characters.prefix(5))
if (senderIdFive > receiverIdFive)
{
self.convoId = senderIdFive + receiverIdFive
}
else
{
self.convoId = receiverIdFive + senderIdFive
}}
I get the error on the let receiverId = receiverData as! String that:
Could not cast value of type 'Chat_App.MessageTableViewCell' (0x10eb2ef10) to 'NSString' (0x110ab1c60).
in a different view controller, I have:
#IBAction func sendMessage(_ sender: Any) {
performSegue(withIdentifier: "sendMessageToUser", sender: self.userpicuid)
}
override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "sendMessageToUser", let chatVc = segue.destination as? SendMessageViewController else {
return
}
chatVc.senderId = self.loggedInUser?.uid
chatVc.receiverData = sender as! String!
chatVc.senderDisplayName = self.userpicuid
chatVc.username = self.username
}
And it segues perfectly.
Sender is Any? and you're casting it to AnyObject. AnyObject refers to a class type, and it's asserting when you attempt to cast it to a Swift value type (String).
Try this instead:
chatVc.receiverData = NSString(string: sender as! String)
Your sender is the the UITableViewCell that initiated the segue. You are crashing when casting it to NSString. Remove this line
let receiverId = receiverData as! String
In prepareForSegue do this instead
chatVc.receiverData = self.userpicuid
i am trying to pass an image from one view controller to another but i am getting an error in prepare for segue function
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let post = posts[indexPath.row]
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as? collectionViewCellBooks {
if let img = booksVC.imageCache.object(forKey: post.imageUrl as NSString) {
cell.configureCell(post: post, img: img)
return cell
}else {
cell.configureCell(post: post)
return cell
}
}
else {
return collectionViewCellBooks()
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showImage", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "showImage"
{
let indexPaths = self.collectionView!.indexPathsForSelectedItems!
let indexPath = indexPaths[0] as IndexPath
let vc = segue.destination as! newViewController
// vc.image = self.posts[(indexPath as NSIndexPath).row]
vc.image = self.posts[indexPath.row]
}
class newViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
var image = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = self.image
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
this is my Post class
class Post {
private var _caption: String!
private var _imageUrl: String!
private var _postKey: String!
var caption: String {
return _caption
}
var imageUrl: String {
return _imageUrl
}
var postKey: String {
return _postKey
}
init(caption: String, imageUrl: String) {
self._caption = caption
self._imageUrl = imageUrl
}
init(postKey: String, postData: Dictionary<String, AnyObject>) {
self._postKey = postKey
if let caption = postData["title"] as? String {
self._caption = caption
}
if let imagesUrl = postData["imageURL"] as? String {
self._imageUrl = imagesUrl
}
}
}
title and imageURL are saved on firebase database
You are getting the error because you are not sending an image to the new viewController but an instance of your Post class, which by the way doesn't even contain an image (only an imageURL). You have to extract the image from the server first before you can parse it anywhere.
You should parse the whole post as you are doing it right now and download the image via the postID directly in the new ViewController. (in case you saved the image in Firebase storage) I always end up parseing the whole object because later in the development you maybe decide to show more properties in the newViewController. If you parsed the whole object you don't have to change your code structure anymore.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "your Identifier" {
if let vc = segue.destination as? NewViewController {
if let post = sender as? Post {
vc.post = post
}
}
}
}
in your didSelectIdemAt function you need to change the sender of the performSegue function:
performSegue(withIdentifier: "your Identifier", sender: post)
now your newViewController has a little bit more code but that is how i do it and it works stable.
let reference = FIRDatabase.database().reference()
reference.child("posts").child("<postID>").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
print(snapshot.value)
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
if let postsDict = snap.value as? Dictionary<String, AnyObject> {
if let imageURL = postsDict["imageURL"] as? String {
let httpsReference = FIRStorage.storage().reference(forURL: imageURL)
httpsReference.data(withMaxSize: 1 * 1024 * 1024, completion: { (data, error) in
if error != nil {
print("error... couldn't get picture from Server \(error.localizedDescription)")
} else {
let image = UIImage(data: data!)
self.img = image! // you need to create this variable somewhere in your viewController Class before this code
//"your UIImageVIew.image" = img AND THAT IS IT
}
}
}
}
}
}
Can you try with following change in preparefor segue method.. let me know
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
{
if segue.identifier == "showImage"
{
let indexPaths = self.collectionView!.indexPathsForSelectedItems!
let indexPath = indexPaths[0] as IndexPath
let vc = segue.destinationViewController as! newViewController
let post = posts[indexPath.row]
if let img = booksVC.imageCache.object(forKey: post.imageUrl as NSString) {
vc.image = img
}
}
}
class newViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
var image = nil
override func viewDidLoad() {
super.viewDidLoad()
if self.image {
self.imageView.image = self.image
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I have LoginViewController where a user logs in. Upon doing this details are fetched of communities they are in.
These names of these communities are passed to ViewController with this section of code:
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
if let arr = json?["communities"] as? [[String:String]] {
self.communitiesArray = arr.flatMap { $0["name"]!}
}
self.performSegue(withIdentifier: "backHome", sender: self.communitiesArray)
and
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "backHome" {
let createViewController: ViewController = segue.destination as! ViewController
createViewController.communities = communitiesArray
}
A UITableView is then used to display the list of communities using this code in ViewController:
var communities = [String]()
#IBOutlet weak var communitiesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.communitiesTableView.delegate = self
self.communitiesTableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.communities.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = self.communities[indexPath.row]
let cell = UITableViewCell()
cell.textLabel?.text = title
return cell
}
If a user selects a community, this opens a new view controller 'ShowCommunityViewController` and passes a variable containing its name:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedCellTitle = self.communities[indexPath.row]
self.performSegue(withIdentifier: "showCommunitySegue", sender: self)
}
However, I also want to pass the 'id' of this community which is also contained within "communities" alongside 'name'
I was hoping I could do something like this in my LoginViewController:
if let arr = json?["communities"] as? [[String:String]] {
self.communitiesArray = arr.flatMap { $0["name"]!
self.communitiesArray = arr.flatMap { $1["id"]!
}
}
self.performSegue(withIdentifier: "backHome", sender: self.communitiesArray)
And then somehow pass both the 'name' and 'id' together from 'ViewController' to my new destination ShowCommunityViewController. But that doesn't exist as Syntax and was just something I made up!
What is the best way to pass the name and id together?
You can zip the two together. i.e.
let names = arr.flatMap { $0["name"]! }
let ids = arr.flatMap { $1["id"]! }
let communities = zip(names, ids)
Create a model object, like the following:
class Community {
let id: String
let name: String
init(id: String, name: String) {
self.id = id
self.name = name
}
}
Once you have your JSON, create an array of Community, instead of just using a String array.
var communities = [Community]()
if let arr = json?["communities"] as? [[String:String]] {
communities = arr.flatMap({ (candidate) -> Community? in
guard let id = candidate["id"], let name = candidate["name"] else { return nil }
return Community(id: id, name: name)
})
}
I am not exactly sure where do you have this data. But this should be passed to your vc, where you can select the community.
After this, instead of keeping track of the selectedCellTitle, keep track of selectedCommunity and assign that to your vc in prepareforsegue.
I have the segue setup as:
and the tableView row selection:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// Ensure controller knows which dataset to pull from,
// so detail view is correct
var friendChat: Friend!
if searchController.active && searchController.searchBar.text != "" {
friendChat = filterMappedFriends[indexPath.row]
} else {
friendChat = mappedFriends[indexPath.row]
}
// Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them:
if(friendChat.statusSort == 2) {
self.performSegueWithIdentifier("showIndividualChat", sender: friendChat)
} else if (friendChat.statusSort == 1) {
print("Can invite to be friend")
} else if (friendChat.statusSort == 0) {
print("Invite to Feast")
}
}
and the prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let indexPath = tableView.indexPathForSelectedRow {
// Ensure controller knows which dataset to pull from,
// so detail view is correct
let friendChat: Friend
if searchController.active && searchController.searchBar.text != "" {
friendChat = filterMappedFriends[indexPath.row]
} else {
friendChat = mappedFriends[indexPath.row]
}
// Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them:
if segue.identifier == "showIndividualChat" {
let controller = segue.destinationViewController as! IndividualChatController
controller.friendChat = friendChat
controller.senderId = Global.sharedInstance.userID
controller.senderDisplayName = Global.sharedInstance.userName
}
}
}
However, the object friendChat, seen in controller.friendChat, of the destination controller is always nil.
How can I pass the data:
controller.friendChat = friendChat
controller.senderId = Global.sharedInstance.userID
controller.senderDisplayName = Global.sharedInstance.userName
to the destination controller successfully?
The first thing you are doing in didSelectRowAtIndexPath is deselecting the row, so when you try and access the selected row in prepareForSegue you are going to get no row selected.
Since you are passing the Friend instance as your sender to performSegueWithIdentifier you can just say let friendChat = sender as? Friend in prepareForSegue;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "showIndividualChat" {
if let friendChat = sender as? Friend {
let controller = segue.destinationViewController as! IndividualChatController
controller.friendChat = friendChat
controller.senderId = Global.sharedInstance.userID
controller.senderDisplayName = Global.sharedInstance.userName
}
}
}
For Swift 3
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.identifier == "showIndividualChat" {
if let friendChat = sender as? Friend {
let controller = segue.destination as! IndividualChatController
controller.friendChat = friendChat
controller.senderId = Global.sharedInstance.userID
controller.senderDisplayName = Global.sharedInstance.userName
}
}
}