Ios 如何将数据从表视图传递到另一个视图控制器
我是一个语言敏捷的新手。。我想将价格从表视图控制器传递到付款视图控制器Ios 如何将数据从表视图传递到另一个视图控制器,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我是一个语言敏捷的新手。。我想将价格从表视图控制器传递到付款视图控制器 import UIKit class MainMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let labeltitle = ["ADLV Black Tee", "ASSC Black Hoodie", "CDG Play Gold Black Tee"] let labelpric
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 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()
}
}
首先您应该创建一个结构
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并将其拖动到目标vc,然后在table dp的内部didSelectRowAt
delegate方法
self.performSegue(withIdentifier:"SegueNameHere",sender:nil)
请共享错误?它显示[String]类型的值没有成员“texr”@Sh_khan请不要。而是编辑问题并添加更多信息。这是否回答了您的问题?但价格不是通行证,如何让它通行@施胡汗