如何将数据从表视图传递到另一个视图控制器
Posted
技术标签:
【中文标题】如何将数据从表视图传递到另一个视图控制器【英文标题】:How to pass the data from table view to another view controller 【发布时间】:2020-05-23 15:56:39 【问题描述】:我是 Swift 语言的新手。我想将价格从表视图控制器传递到支付视图控制器
这是我的表格视图控制器代码
import UIKit
class MainMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
let labeltitle = ["ADLV Black Tee", "ASSC Black Hoodie", "CDG Play Gold Black Tee"]
let labelprice = [("RM 250"), ("RM 305"), ("RM 418")]
let myImage = [UIImage(named: "adlv1"), UIImage(named: "assc"), UIImage(named: "cdg1")]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad()
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
//here got error
override func prepare(for segue: UIStoryboardSegue, sender: (Any)?)
var DestinationViewController : PaymentViewController = segue.destination as! PaymentViewController
if let lText = labelprice.text
DestinationViewController.price = lText
@IBAction func BuyNowbutton(_ sender: Any)
func numberOfSections(in tableView: UITableView) -> Int
return 1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return labeltitle.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainMenuTableViewCell
cell.label11?.text = labeltitle[indexPath.row]
cell.label2?.text = labelprice[indexPath.row]
cell.myImage.image = self.myImage[indexPath.row]
return cell
这是我的付款视图控制器
import UIKit
class PaymentViewController: UIViewController
var items = [item]()
var price : String = ""
@IBOutlet weak var paymentdetails: UILabel!
@IBOutlet weak var cardnametextfield: UITextField!
@IBOutlet weak var validthrutextfield: UITextField!
@IBOutlet weak var cardnumbertextfield: UITextField!
@IBOutlet weak var cvcnumbertextfield: UITextField!
@IBOutlet weak var labelprice: UILabel!
override func viewDidLoad()
super.viewDidLoad()
labelprice.text = price
// Do any additional setup after loading the view.
@IBAction func paybutton(_ sender: Any)
if cardnametextfield.text == ""
alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
else if validthrutextfield.text == ""
alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
else if cardnumbertextfield.text == ""
alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
else if cardnumbertextfield.text == ""
alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
else
alertMessage(titleInput: "Success!", messageInput: "Payment Successful!")
self.transitionToHomePage()
func alertMessage(titleInput: String, messageInput: String)
let alert = UIAlertController(title: titleInput, message: messageInput, preferredStyle: UIAlertController.Style.alert)
let paybutton = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
alert.addAction(paybutton)
self.present(alert, animated: true, completion: nil)
func transitionToHomePage()
let TabHomeViewController = storyboard?.instantiateViewController(identifier: Constrants.Storyboard.TabHomeViewController) as? UITabBarController
view.window?.rootViewController = TabHomeViewController
view.window?.makeKeyAndVisible()
【问题讨论】:
请分享错误? 显示 [String] 类型的值没有成员“texr”@Sh_Khan 请不要repost questions。而是编辑问题并添加更多信息。 这能回答你的问题吗? Passing Data between View Controllers 【参考方案1】:首先你应该创建一个结构
struct Item
let title,price,imageName:String
不是 3 个数组而是 1 个
var arr = [Item]()
然后填写
第二
override func prepare(for segue: UIStoryboardSegue, sender: (Any)?)
var des = segue.destination as! PaymentViewController
if let row = tableView.indexPathForSelectedRow?.row
des.price = arr[row].price
最后从tableView连接segue而不是从cell拖到目标vc,然后在table dp的didSelectRowAt
delegate方法中
self.performSegue(withIdentifier:"SegueNameHere",sender:nil)
【讨论】:
但是价格不及格,如何让价格过关? @Sh_Khan以上是关于如何将数据从表视图传递到另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章