King
King

Reputation: 2025

Passing text to TextField from tableview cell swift

I have an application that displays data in tableview cell and not I want to be able to call the addText viewcontroller when I select the cell and I want to pass the text value in the cell to the text field in the addText viewcontroller

below is what I have tried but the app crashes everytime I select the cell

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

        let list = items?[indexPath.row]

        print("SELECTED ROW \(list?.name)")

        let storyBoard: UIStoryboard = UIStoryboard(name: "AddListSB", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: Constants.ADD_LIST_SB) as! AddListVC

       newViewController.titleTxtField.text = list.title

        self.present(newViewController, animated: true, completion: nil)

    }

it prints the right text in the console though before crashing and does not give the Viewcontroller

Upvotes: 0

Views: 672

Answers (2)

John West
John West

Reputation: 26

Call newViewController.loadViewIfNeeded() before setting the text on the label to make sure the view is loaded.

If this doesn't solve the issue, then the outlets might not be set properly on the view controller.

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100523

The crash because the vc isn't yet loaded

newViewController.sendedText = list.title

//

class AddListVC:UIViewController {
   var sendedText = ""
}

and set the string inside viewDidLoad

Upvotes: 2

Related Questions