gpt4 book ai didi

ios - Swift 2 didSelectRowAtIndexPath UIAlertController 将 segue 推送到带有 4 个字符串的 TVC

转载 作者:行者123 更新时间:2023-11-30 13:21:43 25 4
gpt4 key购买 nike

我在将四个标签的值从警报 Controller 传递到另一个 tableViewController 中的新单元格时遇到一些问题。我不确定我是否使用了最佳方法来将其传递给“添加”操作。

以下是相关片段 ->

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
}

segue“addProduct”是从storyboard中的productTVC绘制到orderVC TableView 作为显示详细信息segue。

我在控制台中得到了一个 LLDB让 orderVC = StoryBoard.instantiateViewControllerWithIdentifier("addProduct") 作为!订单 View Controller 警告:无法从 dyld 共享缓存加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。

我希望有人能帮助我完成这部戏剧,因为我有点迷失了。

最佳答案

我对您试图用代码实现的目标感到非常困惑。但在不尝试进一步分析的情况下,我可以给你一些关于流程应该如何的建议。您应该在此处调用的方法是 prepareForSegue

didSelectRowAtIndexPath触发时,您执行segue addProduct

然后从 prepareForSegue 传递信息

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
}
}

关于ios - Swift 2 didSelectRowAtIndexPath UIAlertController 将 segue 推送到带有 4 个字符串的 TVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37716533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com