简体   繁体   中英

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

The crash because the vc isn't yet loaded

newViewController.sendedText = list.title

//

class AddListVC:UIViewController {
   var sendedText = ""
}

and set the string inside viewDidLoad

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM