EXC_BAD_INSTRUCTION When Passing UICollectionView Cell Data to Different ViewController - ios

I have a UICollectionView that I am populating based on Firebase data. I have created custom cells that populate the UICollectionView:
import UIKit
import Material
class PollCell: CollectionViewCell {
var key: String? {
get {
return self.key
}
}
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var pollQuestion: UILabel!
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I am attempting to access the pollQuestion variable of the clicked cell in the UICollectionView and pass it to another ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPoll" {
let pollViewController = segue.destination as! PollController
if let cell = sender as? PollCell {
pollViewController.passLabel.text = cell.pollQuestion.text
}
}
}
PollController:
import UIKit
class PollController: UIViewController {
#IBOutlet weak var passLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
UPDATE: I have revised the code and am now receiving the error
The app is crashing at runtime, and I am trying to resolve:

The crash occurs because the outlet passLabel is not connected yet when prepare(for segue is called.
You have to declare a (temporary) variable in PollController and set the label in viewDidLoad
class PollController: UIViewController {
#IBOutlet weak var passLabel: UILabel!
var pass = ""
override func viewDidLoad() {
super.viewDidLoad()
passLabel.text = pass
}
...
In prepare(for segue set the variable rather than the text property of the label:
let pollViewController = segue.destination as! PollController
if let cell = sender as? PollCell {
pollViewController.pass = cell.pollQuestion.text
}
}
Note: It's not recommended to gather information from the view (cell). Get the index path and read the information from the model (data source array).

Instead of calling cellForItem, which gives you a new cell, you should use the cell that is passed to you as sender.
if let cell = sender as? PollCell {
pollViewController.passLabel.text = cell.pollQuestion.text
}

Related

Delegate data from one UIViewController to another one

I am completely new to Swift programming and tried to delegate a single String from one ViewController to another by clicking a send button. The problem is , that it does not work ...
I guess it would be easy for you to solve this and considering that it would be very helpful wether you explain me what I did wrong. :)
Thank you a lot
import UIKit
protocol protoTYdelegate {
func didSendMessage(message: String)
}
class New: UIViewController {
#IBOutlet weak var SendButton: UIButton!
var tydelegate: protoTYdelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func SendButtonAction(_ sender: Any) {
let nachricht = "It works fine."
tydelegate?.didSendMessage(message: nachricht)
}
}
import UIKit
class ThankYouPage: UIViewController {
#IBOutlet weak var numbersView: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let controller = New()
controller.tydelegate = self
}
}
extension ThankYouPage: protoTYdelegate{
func didSendMessage(message: String) {
numbersView.text = message
}
As far as I understand, this code block doesn't work but the problem is not in the code, it's actually way that you choose to send data. In iOS development, there are many ways to send data. In your case, you need to use prepareForSegue method to send data to new class, not necessary to use delegates.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "ThankYouPage") {
let vc = segue.destination as! ThankYouPage
vc.message = "Message that you want to send"
}
}
And you need to implement your ThankYouPage as:
class ThankYouPage: UIViewController {
#IBOutlet weak var numbersView: UILabel!
var message = ""
override func viewDidLoad() {
super.viewDidLoad()
numbersView.text = message
}
}
In addition to that, you can use didSet method to print out the message to label instead of printing it directly in viewDidLoad method. Simply:
class ThankYouPage: UIViewController {
#IBOutlet weak var numbersView: UILabel!
var message: String?{
didSet{
numbersView.text = message
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
I hope this helps you.
#Eyup Göymen's answer is right.
I have another way, assuming that you are not using segue and you are pushing to next controller by manual-code.
So your ThankYouPage code should be like :
import UIKit
class ThankYouPage: UIViewController {
#IBOutlet weak var numbersView: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func someButtonAction(_ sender: Any) { // By clicking on some, you are opening that `New` controller
let detailView = self.storyboard?.instantiateViewController(withIdentifier: "New") as! New
detailView.tydelegate = self
self.navigationController?.pushViewController(detailView, animated: true)
}
}
extension ThankYouPage: protoTYdelegate {
func didSendMessage(message: String) {
numbersView.text = message
}
}

Pass data from containerView to MainViewControler

I have this code:
MainViewControler:
class MainViewControler: UIViewController, ContainerToMaster {
#IBOutlet weak var systemContainerView: UIView!
#IBOutlet weak var favoriteProductBtn2: UIButton!
weak var containerViewController: Calculator1ViewController?
func changeBtn() {
favoriteProductBtn2.isHidden = true
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
}
}
CallculatorViewController:
#IBOutlet weak var containerView: UIView!
class CallculatorViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
Calculator1ViewController:
protocol ContainerToMaster {
func changeBtn()
}
class Calculator1ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
containerToMaster?.changeBtn()
}
}
I have such an application layout:
Main view = MainViewControler.
In MainViewControler, I have a containerView (systemContainerView) in which the MainViewControler is located
In the CallculatorViewController, I have the next containerView in which the Calculator1ViewController is located.
When entering the Calculator1ViewController the function: containerToMaster?.changeBtn() should start (it should work in MainViewControler).
The problem is - this function does not work :(
Does anyone know how to fix it?
A contained viewController is embedded with an embed segue. You need to override prepare(for:sender) and set self as the containerToMaster delegate in the destination viewController. Your situation is complicated by the fact that you have a container view embedded in another container view, so you'll need to set up two delegates and pass the button call back:
protocol ContainerToMaster {
func changeBtn()
}
class MainViewControler: UIViewController, ContainerToMaster {
#IBOutlet weak var systemContainerView: UIView!
#IBOutlet weak var favoriteProductBtn2: UIButton!
weak var containerViewController: Calculator1ViewController?
func changeBtn() {
favoriteProductBtn2.isHidden = true
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? CallculatorViewController {
destVC.containerToMaster = self
}
}
}
class CallculatorViewController: UIViewController, ContainerToMaster {
weak var containerToMaster: ContainerToMaster?
override func viewDidLoad() {
super.viewDidLoad()
}
func changeBtn() {
// pass the button call back
containerToMaster?.changeBtn()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? Calculator1ViewController {
destVC.containerToMaster = self
}
}
}
class Calculator1ViewController: UIViewController {
weak var containerToMaster: ContainerToMaster?
override func viewDidLoad() {
super.viewDidLoad()
containerToMaster?.changeBtn()
}
}

Pass Firebase Data from TableViewCell to ViewController

I've been trying multiple ways to pass my firebase data from a TableViewCell to a ViewController and can't figure out the simplest solution.
I have an EncounterTableViewController with EncounterTableViewCells. This is populated with Firebase data. When an EncounterTableViewCell is selected I want to pass the data to the EncounterDetailViewController.
EncountersTableViewController.swift
class EncountersTableViewController: UITableViewController {
var encounters : [Encounter] = []
// MARK: - View Did load
override func viewDidLoad() {
super.viewDidLoad()
showAllEncounters()
}
// MARK: - Encounter filters
func showAllEncounters() {
// Firebase tableview data
FIRDatabase.database().reference().child("encounters").observeSingleEvent(of: .value, with: { (snapshot) in
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let restDict = rest.value as? [String: Any] else { continue }
let encounter = Encounter()
encounter.sharkName = (restDict["shark_name"] as? String)!
encounter.date = (restDict["trip_date"] as? String)!
encounter.contributorName = (restDict["contributor"] as? String)!
encounter.contributorImage = (restDict["contributor_image"] as? String)!
let mediaDict = (restDict["media"] as? NSArray)
let firstImage = mediaDict![0] as! NSDictionary
encounter.mainImage = firstImage["thumb_url"] as! String
self.encounters.append(encounter)
self.tableView.reloadData()
}
})
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToEncounterCard" {
let destination = segue.destination as! EncounterDetailViewController
}
}
EncounterDetailViewController.swift
class EncounterDetailViewController: UIViewController {
// MARK: - Outlets
#IBOutlet weak var encounterDateLabel: UILabel!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var lengthLabel: UILabel!
#IBOutlet weak var contributorNameLabel: UILabel!
#IBOutlet weak var contributorImageView: UIImageView!
// MARK: - Properties
var dictionary: [String:AnyObject]?
// MARK: - View did load
override func viewDidLoad() {
super.viewDidLoad()
}
}
Instead of passing dictionary you need to pass object of Encounter, so declare one instance property of type Encounter in your EncounterDetailViewController with named selectedEncounter. Now in prepareForSegue method of EncountersTableViewController pass the object of Encounter from array to this detailViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToEncounterCard" {
let destination = segue.destination as! EncounterDetailViewController
if let indexPath = self.tableView.indexPathForSelectedRow {
destination.selectedEncounter = self.encounters[indexPath.row]
}
}
}
EncounterDetailViewController
class EncounterDetailViewController: UIViewController {
// MARK: - Outlets
#IBOutlet weak var encounterDateLabel: UILabel!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var lengthLabel: UILabel!
#IBOutlet weak var contributorNameLabel: UILabel!
#IBOutlet weak var contributorImageView: UIImageView!
// MARK: - Properties
var selectedEncounter: Encounter?
// MARK: - View did load
override func viewDidLoad() {
super.viewDidLoad()
//access selectedEncounter property here and set all label's text
}
}
In EncountersTableViewController add the tableView delegate method, func tableView(UITableView, didSelectRowAt: IndexPath) and inside this method, you can get the selected encounter from encounter array by using indexPath.row. After you get the encounter you can performSegue(withIdentifier: "segueToEncounterCard" , sender: encounter ).
And in prepareForSegue method, you can get the encounter by casting sender value as Encounter object and you can pass the encounter to destination.

prepareForSegue function not working

I am sure I have two view controllers, that have the appropriate files as their classes. (ViewController1.swift and ViewController2.swift) I am trying to pass data from one of the view controllers to the other one with the following code; but I get the error "UIView Controller is not convertible to ViewController2". What should I do? I tried to change as to as! and var to let; neither worked.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
if (segue.identifier == "btnSubmitSegue") {
var svc = segue.destinationViewController as ViewController2;
svc.DataPassed = textField.text
}
}
the code for ViewController2
import UIKit
class ViewController2: UIViewController {
var toPass: String = ""
#IBOutlet weak var labeld: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.labeld.text = "passed: \(self.toPass)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
the code for ViewController1
import UIKit
class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var textField: UITextField!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TheSegue" {
let vc = segue.destinationViewController as! ViewController2
vc.toPass = self.textField.text!
}
}
}
The following example works fine for me. Code for the first view controller:
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TheSegue" {
let vc = segue.destinationViewController as! ViewController2
vc.passedText = self.textField.text!
}
}
}
And in the second view controller:
class ViewController2: UIViewController {
var passedText: String = ""
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.label.text = "passed: \(self.passedText)"
}
}
Don't forget to name the segue in the Storyboard:
Your destination view controller is referring to the wrong controller. You should have this in your code, not as! ViewController1:
let vc = segue.destinationViewController as! ViewController2
Also need to fix your IBOutlet name, the vc.toPass = self.textField.text! should be:
vc.toPass = self.labeld.text!
Make sure the custom class is set for the second view controller:
It may just be that ViewController2 is not declared as a subclass as UIViewController.
At the top of the ViewController2 file you would declare it as follows:
class ViewController2: UIViewController {
\\ ...
}
Have you set the name in the Storyboard ? Under custom class, Class needs to point to the view controllers associated class.
this class is not key value coding-compliant for the key
labelPassedData.' But I do not even have a variable like
labelPassedData
Click you view controller and press right hand arrow button, it shows all connections.
I guess you copy and pasted the view controllers and hence you get this problem.

Passing Data in Swift Between View Controllers in Same File

Why can't I pass data from one view controller class to another view controller class when they are in the same .swift file?
I've done both of the tutorials linked bellow in an effort to understand passing data, and both have you make another .swift file for you second view controller. When I followed them exactly it works. When I do everything the same but just put the class SecondViewController in the same .swift file as the class FirstViewController it doesn't work. Why?
I'm building an application with three view controllers and I'd rather just keep all the code in one file if I can. Is this why it's not working or is it just bad form?
Tutorials:
How to Pass Data from View Controller (Swift : Xcode) on YouTube
Segue Between Swift View Controllers on CodingExplorer
This works:
// ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet var TextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController: ViewTwo = segue.destinationViewController as ViewTwo
DestViewController.LabelText = TextField.text
}
}
// ViewTwo.swift
import UIKit
class ViewTwo: UIViewController {
#IBOutlet var Label: UILabel!
var LabelText = String()
override func viewDidLoad() {
Label.text = LabelText
}
}
This does not:
// ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet var TextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController: ViewTwo = segue.destinationViewController as ViewTwo
DestViewController.LabelText = TextField.text
}
}
class ViewTwo: UIViewController {
#IBOutlet var Label: UILabel!
var LabelText = String()
override func viewDidLoad() {
Label.text = LabelText
}
}
Working fine for me.
Have you set the classes for your views properly? The view with your textField (and probably a button to ViewTwo) should be set to ViewController and your second view with the label should be set to ViewTwo.
Check the connections between your label/textField. Are they properly connected to your class?
This is the code I used, which should be exactly the same:
import UIKit
class ViewController: UIViewController {
#IBOutlet var TextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController = segue.destinationViewController as! ViewTwo
DestViewController.LabelText = TextField.text
}
}
class ViewTwo: UIViewController {
#IBOutlet var Label: UILabel!
var LabelText = String()
override func viewDidLoad() {
Label.text = LabelText
}
}

Resources