0

I am new in programming, and have problem. I am using parse for my array in tableview. When the row is selected i want to segue on another view controller to textView. The tableview works good but i can't get text to textView.

tableVC:

import UIKit
import Parse
class ThirdTableVC: UITableViewController {
@IBOutlet weak var refresherQuotes: UIRefreshControl!
@IBOutlet var quoteTable: UITableView!

var selectedQuote: PFObject?
var quoteItems = [PFObject]()

override func viewDidLoad() {
    super.viewDidLoad()

}
@IBAction func updateQuotesResults(_ sender: UIRefreshControl) {
    fetchQuotesData()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    loadQuoteTexts(selectedQuote: selectedQuote)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {



    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



    return quoteItems.count
}

func fetchQuotesData() {
    let quotesQuery = PFQuery(className: "TotalTest")
    quotesQuery.whereKey("Subcategory", equalTo: selectedQuote ?? nil)
    quotesQuery.findObjectsInBackground { (objects, error) in
        if let realCategoryObjects = objects {
            self.quoteItems = realCategoryObjects
            self.tableView.reloadData()
            self.refresherQuotes.endRefreshing()
        }
    }
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let quoteCell = tableView.dequeueReusableCell(withIdentifier: "quoteCell", for: indexPath)

    let quoteItem = quoteItems[indexPath.row]
    let quoteUserTitle = quoteItem.object(forKey: "TextQuote") as? String

    quoteCell.textLabel?.text = quoteUserTitle

    return quoteCell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showQuoteDetail" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let quoteobject = quoteItems[indexPath.row] as? NSDate
            let quoteController = (segue.destination as! UINavigationController).topViewController as! DetailViewController
            quoteController.detailItem = quoteobject
            quoteController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
            quoteController.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Row tapped: \(indexPath.row)")

    let selectedQuotes: PFObject = quoteItems[indexPath.row]
    let  FourthVC = self.storyboard?.instantiateViewController(withIdentifier: "FourthViewController") as! FourthViewController
    FourthVC.fourthTextView.text = quoteItems[indexPath.row] as? String
    self.navigationController?.pushViewController(FourthVC, animated: true)


}

func loadQuoteTexts(selectedQuote: PFObject!) {

    let quoteQuery = PFQuery(className: "TotalQuote")
    quoteQuery.whereKey("QuoteSubs", equalTo: selectedQuote ?? nil)
    quoteQuery.includeKey("QuoteSubs")

    quoteQuery.findObjectsInBackground { (result: [PFObject]?, error) in
        if let searchQuoteResults = result {

            self.quoteItems = searchQuoteResults
            self.quoteTable.reloadData()
        }
    }   
}
}

How can I change this? viewController with textView:

 import UIKit
 import Parse

 class FourthViewController: UIViewController {

var getQuote: PFObject?

@IBOutlet weak var fourthTextView: UITextView!
@IBOutlet weak var fourthLabel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    fourthLabel.text! = getQuote as! String
    fourthTextView.text! = getQuote as! String 
}
}

Please help me to passing texts

Bogdan59
  • 31
  • 9
  • Is **let selectedQuotes: PFObject = quoteItems[indexPath.row]** returns nil – Alizain Prasla Nov 16 '17 at 13:47
  • I agree with @AlizainPrasla, you should check in your `tableView didSelectRowAt` function that your `selectedQuotes` object is what you expect it to be. Also, what's your `prepareForSegue` function doing? It seems to be a duplicate attempt at the same? Are they both being called or maybe not the one you're expecting? – Terje Nov 16 '17 at 13:49

2 Answers2

0

The problem is that you are changing from a view to another with pushViewController, by doing that your prepareForSegue won't be executed.

On your didSelectRow you need to call performSegue(withIdentifier:sender:).

You can lookup this question for more information on how to do it.

jvrmed
  • 834
  • 6
  • 12
0

If you use pushViewController do it like that , in did selectRowAt

let MainStory: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desVC = MainStory.instantiateViewController(withIdentifier: "FourthViewController") as! FourthViewController

and now pass your text

desVC.getText = "here goes your text u want to pass"

FourthViewController set up your var

var getText = String()

so you can finally use

self.navigationController?.pushViewController(desVC, animated: true)

so it will pass all parameters you previous add with desVC.getSomething

in FourthViewController you just need to use getText.

Bozzo
  • 318
  • 1
  • 3
  • 13