0
votes
addQuestionViewTableView Controller (This view controller is segued(push) to SubViewController)
  import UIKit

class SubjectsTableViewCell: UITableViewCell {
    @IBOutlet weak var subjectImage: UIImageView!
    @IBOutlet weak var subjectName: UILabel!
}

class AddQuestionTableViewController: UITableViewController {

    var Subjects = ["Geology", "Mathematics", "Computer", "English", "History", "Science"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (Subjects.count)
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "pickedSubjectSegue", sender: Subjects[indexPath.row])
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let next = segue.destination as! SubViewController

        next.text = sender as! String
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SubjectsTableViewCell


        cell.subjectName?.text = Subjects[indexPath.row]
        cell.subjectImage?.image = UIImage(named: (Subjects[indexPath.row] + ".jpeg"))

        return cell
    }

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

My Second View Controller (i added nav bar + button and ctrl drag to segue(modally) to nav bar controller of addViewController). I am using navigation bar item action to pass data(is this okay?)

 import UIKit

    class SubViewController: UIViewController {


        @IBOutlet weak var subImage: UIImageView!

        @IBOutlet weak var subTitle: UILabel!

        @IBOutlet weak var subCount: UILabel!

        var text = "hi"

        override func viewDidLoad() {
            super.viewDidLoad()

            subTitle.text = text // name of the subject - success
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }




        @IBAction func add(_ sender: Any) {
              self.performSegue(withIdentifier: "addQuestion", sender: text)
        }

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let next2 = segue.destination as? AddViewController{
            next2.text2 = sender as! String
            }


  }
}

My Third View Controller

import UIKit
import Firebase
import FirebaseDatabase

class AddViewController: UIViewController {


    @IBOutlet weak var subjectLabel: UILabel!

    @IBOutlet weak var questionField: UITextField!
    @IBOutlet weak var correctField: UITextField!
    @IBOutlet weak var optionAField: UITextField!
    @IBOutlet weak var optionBField: UITextField!
    @IBOutlet weak var optionCField: UITextField!


    var text2 = "hello"
    var ref: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        ref = FIRDatabase.database().reference()
        self.hideKeyboard()

        subjectLabel.text = text2 // i want this to be the name of subject also

    }

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


    @IBAction func cancel(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    @IBAction func add(_ sender: Any) {
        if questionField.text != "" && correctField.text != "" && optionAField.text != "" && optionBField.text != "" && optionCField.text != "" {

            //saving to firebaseDB
            self.ref?.child("\(text2)").childByAutoId().setValue(["Question": questionField.text, "Answer": correctField.text, "optionA": optionAField.text, "optionB": optionBField.text, "optionC": optionCField.text]) //text2 should be the name of the subject 

            alert(title: "Success!", message: "Question has been added to database.")
            questionField.text = ""
            correctField.text = ""
            optionAField.text = ""
            optionBField.text = ""
            optionCField.text = ""
        }
        else
        {
            alert(title: "Error", message: "Missing fields. Please input")
        }

    }


    func alert(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        alertController.addAction(action)

        self.present(alertController, animated: true, completion: nil)
    }
}
1

1 Answers

0
votes

You just pass text as sender in SubViewController.

@IBAction func add(_ sender: Any) {
   self.performSegue(withIdentifier: "addQuestion", sender: text)
}