Nate Grant
Nate Grant

Reputation: 13

When uitableviewcell is clicked seque to viewcontroller and fill uitextfield with the text in the cell selected swift

I'm trying to make it so when I click a tableviewcell in my tableviewcontroller it segues to another viewcontroller and uses the text that was in the cell and automatically fills the uitextfield in the controller that it was segued too.

I was thinking it would start from this method

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {



}

Exercise TableView Controller

Clicked in this controller and uses the text in the cell

Add Exercise TableView Controller

Fills the exercise uitextfield with text

In Swift btw. Thanks.

Upvotes: 0

Views: 60

Answers (1)

Kerby Jean
Kerby Jean

Reputation: 207

  1. Create a Segue in the storyboard and give it an identifier("YourSegue")
   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

  let text = "Your text"
    performSegueWithIdentifier("YourSegue", sender: text)
}
  1. Then Call This function
 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                if segue.identifier == "YourSegue" {
                   let yourOtherVC = segue.destinationViewController as? YourOtherViewController
                if let text = sender as? String {
                yourOtherViewController?.text = text    
       }  
     } 
    }
  1. Put this variable in the ViewController that you want to show(YourOtherViewController).
var text: String

override func viewDidLoad() {
super.viewDidLoad() 

if text != nil {
yourTextField.text = text

  }

}

Upvotes: 1

Related Questions