I'm having some trouble passing the value of four labels from my alert Controller to a new cell in another tableViewController. I'm not sure I'm using the best method to pass it on the "add" action.
Here are the relevant snippets ->
ProductTableViewController.swift
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ProductTableViewCell
// Get the row data for the selected row
let product = frc.objectAtIndexPath(indexPath) as! ProductItem
cell.productcodeLabel.text = product.productcode
cell.detailLabel.text = product.detail
cell.quantityLabel.text = "MOQ \(product.quantity as! Double)"
cell.barcodeLabel.text = product.barcode
//MARK: - Add Item Alert
var quantityTextField = UITextField()
let alertController = UIAlertController(title: "\(product.detail!)\n \("MOQ \(product.quantity as! Double)")", message: nil, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: {(alert: UIAlertAction!) in print("Cancel Button Pressed")
alertController.dismissViewControllerAnimated(true, completion: nil)
})
let action = UIAlertAction(title: "Add", style: .Default, handler: { (action) -> Void in
// Now do whatever you want with inputTextField (remember to unwrap the optional)
quantityTextField = alertController.textFields![0] as UITextField
print("Add Button Pressed")
print("You entered \(product.productcode!) \(quantityTextField.text!)")
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let orderVC = storyBoard.instantiateViewControllerWithIdentifier("addProduct") as! OrderViewController
orderVC.productcodeString = product.productcode! as String!
orderVC.detailString = product.detail! as String!
orderVC.quantityString = quantityTextField.text! as String!
orderVC.barcodeString = product.barcode! as String!
self.navigationController?.pushViewController(orderVC, animated: true)
})
alertController.addTextFieldWithConfigurationHandler{ (quantityTextField) -> Void in
quantityTextField.placeholder = "Enter quantity here..."
quantityTextField.font = UIFont.systemFontOfSize(15)
quantityTextField.autocorrectionType = UITextAutocorrectionType.No
quantityTextField.keyboardType = UIKeyboardType.NumberPad
quantityTextField.returnKeyType = UIReturnKeyType.Done
quantityTextField.clearButtonMode = UITextFieldViewMode.WhileEditing;
quantityTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
quantityTextField.delegate = self
}
alertController.addAction(cancelAction)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion:{})
}
OrderViewController.swift
class OrderViewController: UITableViewController{
var productcodeString = String()
var detailString = String()
var quantityString = String()
var barcodeString = String()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("OrderCell", forIndexPath: indexPath) as! OrderTableViewCell
// Configure the cell...
cell.productcodeLabel.text = productcodeString
cell.detailLabel.text = detailString
cell.quantityLabel.text = "X \(quantityString)"
cell.barcodeLabel.text = barcodeString
return cell
}
The segue "addProduct" is drawn from productTVC to orderVC tableviews in the storyboard as a show detail segue.
I get a LLDB in console on the line let orderVC = storyBoard.instantiateViewControllerWithIdentifier("addProduct") as! OrderViewController warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.
I hope somebody can help me with this drama as im a little lost.
I am quite confused by what you are trying to achieve with your codes. But without trying to analyse it further I can give you some advise on how the flow should be. The method you should be calling here is prepareForSegue
when didSelectRowAtIndexPath
fires, you perform the segue addProduct
then from prepareForSegue
you pass the information
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "addProduct") {
let destination = segue.destinationViewController as! OrderViewController
let indexPath = tableView.indexPathForSelectedRow
let product = frc.objectAtIndexPath(indexPath) as! ProductItem
destination.productcodeString = product.productcode as! String
}
}
Hi Chris, thanks for the solution you posted. I tried what you offered and not only did it throw an error as well but my scanner segue broke also. I thought a picture may clarify what I am trying to figure out. As you can see when a cell is selected, an alert view with text box pops up to accept a quantity and the Add button is pressed to invoke the segue to OrderViewController with all the data. I can't even get it to segue without data from the alertView.
I'm going to try build an orderDelegate next and focus on just getting the segue to fire without crashing.
Finally got the segue to work properly now! Here is the final config that actually passes the four strings. Hope it helps someone else...
In the OrderViewController:
var passedProductcode = String()
var passedDetail = String()
var passedQuantity = String()
var passedBarcode = String()
DidSelectRowAtIndex Method in ProductTableViewController:
var selectedPC:String?
var selectedD:String?
var selectedQ:String?
var selectedBC:String?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true) //Required!
let product = frc.objectAtIndexPath(indexPath) as! ProductItem
var quantityTextField = UITextField()
let alertController = UIAlertController(title: "\(product.detail!)" , message: "\("Minimum Order Quantity = \(product.quantity!)")", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: {(alert: UIAlertAction!) in print("Cancel Button Pressed")
alertController.dismissViewControllerAnimated(true, completion: nil)
})
let action = UIAlertAction(title: "Add", style: .Default, handler: { action in
quantityTextField = alertController.textFields![0] as UITextField
//print("Add Button Pressed")
//print("You entered \(product.productcode!) \(quantityTextField.text!)")
self.selectedPC = product.productcode!
self.selectedD = product.detail!
self.selectedQ = quantityTextField.text!//product.quantity!
self.selectedBC = product.barcode!
//print("DSRAIP: \(self.selectedPC!)")
//print("DSRAIP: \(self.selectedD!)")
//print("DSRAIP: \(self.selectedQ!)")
//print("DSRAIP: \(self.selectedBC!)")
self.performSegueWithIdentifier("addProduct", sender: self)
})
alertController.addTextFieldWithConfigurationHandler{ (quantityTextField) -> Void in
quantityTextField.placeholder = "Enter quantity here..."
quantityTextField.font = UIFont.systemFontOfSize(15)
quantityTextField.autocorrectionType = UITextAutocorrectionType.No
quantityTextField.keyboardType = UIKeyboardType.NumberPad
quantityTextField.returnKeyType = UIReturnKeyType.Done
quantityTextField.clearButtonMode = UITextFieldViewMode.WhileEditing;
quantityTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
quantityTextField.delegate = self
}
alertController.addAction(cancelAction)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion:{})
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "scanner" {
let scannerViewController: ScannerViewController = segue.destinationViewController as! ScannerViewController
scannerViewController.delegate = self
print ("Scanner Segue!")
} else if segue.identifier == "addProduct" {
print ("Add Item Segue!")
let destination = segue.destinationViewController as! OrderViewController
//print("Segue: \(selectedPC!)")
//print("Segue: \(selectedD!)")
//print("Segue: \(selectedQ!)")
//print("Segue: \(selectedBC!)")
destination.passedProductcode = (selectedPC)!
destination.passedDetail = (selectedD)!
destination.passedQuantity = (selectedQ)!
destination.passedBarcode = (selectedBC)!
}
}
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.