I'm trying to figure out how I can take the current text in the cell that is selected and use it in a uitextfield when I segue to another tableview controller.
Here's my code
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("addPRTableViewController") as! addPRTableViewController
self.navigationController?.pushViewController(controller, animated: true)
}
That's the segue.
Here's my array which if it is cell [0] that is selected I'd like it to fill the uitextfield txtPR with the string.
var abExercises = ["Ab Roller", "Crunches", "Flat Lying Bench Raise", "Leg Raises", "Russian Twist", "Weighted Crunch"]
Updated Code
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "YourSegue"
{
let nextScene = segue.destinationViewController as! addPRTableViewController
if let indexPath = abTable.indexPathForSelectedRow
{
let exercise = abExercises[indexPath.row]
nextScene.txtPR.text = exercise
}
}
}
Destination tableview controller (addPRTableViewController)
import UIKit
class addPRTableViewController: UITableViewController, UITextFieldDelegate {
@IBOutlet var txtPR: UITextField!
@IBOutlet var txtDesc: UITextField!
@IBOutlet weak var txtWeight: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
txtPR.delegate = self
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 5
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool
{
let maxLength = 25
let currentString: NSString = textField.text!
let newString: NSString =
currentString.stringByReplacingCharactersInRange(range, withString: string)
return newString.length <= maxLength
}
//Button Clicked
@IBAction func btnAddTask(sender : UIBarButtonItem){
if (txtPR.text == ""){
//Task Title is blank, do not add a record
} else {
//add record
let name: String = txtPR.text!
let description: String = txtDesc.text!
let weight: String = txtWeight.text!
taskMgr.addTask(name, desc: description, weight: weight)
//dismiss keyboard and reset fields
self.view.endEditing(true)
txtPR.text = nil
txtDesc.text = nil
txtWeight.text = nil
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool{
textField.resignFirstResponder()
return true
}
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView //recast your view as a UITableViewHeaderFooterView
header.textLabel!.textColor = UIColor(red: 246/255, green: 75/255, blue: 55/255, alpha: 1.0) //make the text white
}
}
You should look at initiating your segue through the prepareForSegue
method. You would need to create a variable in the destination viewController to hold the data passed to it.
Set up a segue from the UITableViewCell to the destination view controller and give the segue an appropriate identifier.
The use the following method to pass the data when the segue is called by tapping the cell.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "yourSegueName"
{
let nextScene = segue.destinationViewController as! NameOfYourViewController
if let indexPath = yourTableOutlet.indexPathForSelectedRow
{
let exercise = abExercises[indexPath.row]
nextScene.variableCreatedAtDestination = exercise
}
}
}
You then do not need to call the segue in didSelectRowAtIndexPath
Update
Ok, create a variable in addPRTableViewController called var prexercise = String()
. In the original viewcontroller, in the prepareForSegue
, change nextScene.txtPR.text = exercise
to nextScene.prexercise = exercise
.
In the addPRTableViewController, in the viewDidLoad
(or whenever you want the data to appear), set this textPR.text = prexercise
. Let me know how that works out for you.
Instead of setting the text directly, you should use a variable to store the data that you want to set to the text field. And then in your viewDidLoad()
func set the text using that variable. Here is an example:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "YourSegue"
{
let nextScene = segue.destinationViewController as! addPRTableViewController
if let indexPath = abTable.indexPathForSelectedRow
{
let exercise = abExercises[indexPath.row]
nextScene.txt = exercise
}
}
}
Destination tableview controller (addPRTableViewController)
class addPRTableViewController: UITableViewController, UITextFieldDelegate {
@IBOutlet var txtPR: UITextField!
@IBOutlet var txtDesc: UITextField!
@IBOutlet weak var txtWeight: UITextField!
var txt:String = ""
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
txtPR.delegate = self
txtPR.text = txt
}
Hope this helps. :)
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.