How to Pass Text from Cell to Textview in Another View Controller

How to pass text from cell to textView in another View Controller?

Don't pass text here create a variable in your NoteDetailsViewController

var passedNote: String = ""

Then use it here

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let vc = storyboard?.instantiateViewController(withIdentifier: "NoteDetailsViewController") as! NoteDetailsViewController
vc.passedNote = notes[indexPath.row]
self.show(vc, sender: self)

}

Check it in your viewDidLoad method that you got passed data or not

override func viewDidLoad() {
super.viewDidLoad()

print(passedNote) // check here you will get data or not then add it to textView
NoteDetailsOutlet.text = passedNote
}

ExtraNote: Following the naming conventions for variables should start with small letter such as noteDetailsOutlet instead of NoteDetailsOutlet.

Swift - Passing data form UITextField to TextView in another viewController

First ViewController

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var txtView: UITextView! //your textView

override func viewDidLoad() {
super.viewDidLoad()

}

//MARK: - Button Action
@IBAction func btnClick(_ sender: UIButton) {

let userDefaultStore = UserDefaults.standard //userDefault object
userDefaultStore.set(txtView.text, forKey: "key_Value") //store textView value in userDefault

//navigate secondViewController
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
}
}

Second ViewController

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var txtView: UITextView! // your textView
override func viewDidLoad() {
super.viewDidLoad()

let userDefault = UserDefaults.standard //create UserDefault object

txtView.text = userDefault.string(forKey: "key_Value")!//get userdefault value using same key which used to store date and set in textView
txtView.isUserInteractionEnabled = false
}
}

How to change textView in separate ViewController from button in another ViewController?

Follow these steps...

1: Create a Separate file called Manager.swift and place this code in it...

//manager.swift

import Foundation

struct Manager {

static var messageText = [String]()

}

2: Clean your project by pressing Shift+Command+K.

3: In the first view controller set the messageText to the text fields text...

Manager.messageText.append(self.textField.text)

4: In the second view controller retrieve the text and set the label to the message text...

self.label.text = Manager.messageText[0]

5: Your Finished!!

Parse from TableVC to textView in other viewController swift

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.

Passing data from table view to textview of view controller

Here is your complete Updated code for one tableView to detailView:

import UIKit

class FirstTableViewController: UITableViewController{

var FirstTableArray = [String]()
var passThisArray = [String]()

override func viewDidLoad() {
super.viewDidLoad()
// This array will display on your tableviewcell.
FirstTableArray = ["First", "Second", "Third","Fourth"]

//You can pass element of this array
passThisArray = ["element1", "element2", "element3", "element4"]

}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

Cell.textLabel?.text = passThisArray[indexPath.row]

return Cell

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
var vc = segue.destinationViewController as! ViewController

//Get the Index of selected Cell
var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!

//assign string to next view controller instance from selected cell.
vc.FirstString = FirstTableArray[indexPath.row]
}
}
}

And you have to modify many things from your project.

And HERE is your project.

Adding another cell to tableview when done editing textview inside cell

First at all you have to create a way to communicate between your cell and view controller.
You can use delegate pattern or callbacks for this.
For example:

final class TextFieldCell: UITableViewCell {

// MARK: - IBOutlets
@IBOutlet weak var textField: UITextField!

// MARK: - Local variables
var callback: ((_ text: String) -> Void)?

// MARK: - Lyfecycle
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
}

Also don't forget to call our callback:

extension TextFieldCell: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
callback?(textField.text!)
return true
}
}

Great! Now we send our string from cell to controller!

Example of code for your view controller(simplified version):

class ViewController: UIViewController {
// MARK: - IBOutlets
@IBOutlet weak var tableView: UITableView!

// MARK: - Local variables
var titles = ["Hello", "world"]
}

// MARK: - UITableViewDataSource

extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}

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

textFieldCell.textField.placeholder = titles[indexPath.row]
textFieldCell.callback = { [weak self] newTitle in // don't forget about memory leaks
guard let `self` = self else { return }

// calculating index path for new row
let newIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)

// appending a new row in table view
self.titles.append(newTitle)
self.tableView.insertRows(at: [newIndexPath], with: UITableView.RowAnimation.automatic)
}

return textFieldCell
}
}

How to access textview value which is inside tableview cell in swift 5?

You need to get the indexPath of the cell whose text you want to get
get the cell for that indexpath like

@IBAction func btnSave(_ sender: Any) {
let indexPath = IndexPath(row: 0, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? SummaryManualEditTableCell {
let text = cell.txtAnswers.text
}
}

And if you have multiple cells with textFields you can loop around to get all fields

  @IBAction func btnSave(_ sender: Any) {
var allTextViewsText = ""
for i in 0...5{
let indexPath = IndexPath(row: i, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? SummaryManualEditTableCell {
allTextViewsText += cell.txtAnswers.text
}
}
print(allTextViewsText)
}

But keep it in mind that this approach only works in the case of visible cells otherwise for non visible cells you will get nil

I will suggest you to implement textView:shouldChange in each cell who has textView with a delegate to the tableView's viewController. When text is changed in the cell, delegate should propagate the change to the viewController which will save the value in a variable.

Then, when you press on the save button, you would simply take values from variables.



Related Topics



Leave a reply



Submit