[英]Pass textfield to a label in a second view controller in swift
I am making a simple client invoice list using a tableView with custom labels in each row. 我正在使用tableView制作简单的客户发票清单,每行都有自定义标签。 at the top you click the plus sign.
在顶部,单击加号。 This takes you to a second page where you type in your clients name.
这将带您进入第二页,在其中键入客户名称。 you hit save and it should add it to the label in the first view controller.
您单击保存,它将其添加到第一个视图控制器中的标签中。 I cannot get this to work.
我无法使它正常工作。
my tableview storyboard has the file called ViewController.swift
- in it 我的tableview故事板中有一个名为
ViewController.swift
的文件-
import UIKit
var invoiceList = [""]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var clientTableView: UITableView!
@IBOutlet var clientLabel: UILabel!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return invoiceList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "clientItem", for: indexPath) as! UITableViewCell
let ClientName = invoiceList[indexPath.row]
cell.textLabel?.text = [clientInput]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete
{
invoiceList.remove(at: indexPath.row)
clientTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
clientTableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
My second storyboard where you type in the text field. 您在文本字段中输入的第二个故事板。 File is called
AddInvoiceItem.swift
. 该文件称为
AddInvoiceItem.swift
。 in it - 在里面 -
import UIKit
class AddInvoiceViewController: UIViewController {
var clientTextField:String!
@IBOutlet var clientInput: UITextField!
@IBAction func addItem(_ sender: Any) {
if clientInput.text != ""
{
invoiceList.append(clientInput.text!)
clientInput.text = ""
_ = navigationController?.popViewController(animated: true)
}
}
@IBAction func cancelBtn(_ sender: Any) {
_ = navigationController?.popViewController(animated: true)
}
}
My error keeps happening when I am trying to use the code line 当我尝试使用代码行时,我的错误不断发生
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "clientItem", for: indexPath) as! UITableViewCell
let ClientName = invoiceList[indexPath.row]
cell.textLabel?.text = [clientInput]
return cell
}
The cell.textLabel?.text = [clientInput]
I know this is wrong, but cannot figure it out! cell.textLabel?.text = [clientInput]
我知道这是错误的,但无法弄清楚!
Make a new cell class 制作一个新的细胞班
class CustomCell: UITableViewCell {
//Make your outlets here, connect the outlets from cell in your storyboard like ClientNameLabel
}
Please select the cell on your Storyboard, and enter the name of your class in this field. 请选择情节提要上的单元格,然后在此字段中输入班级名称。 In your case it would be CustomCell
您的情况是CustomCell
then in ViewController 然后在ViewController中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "clientItem", for: indexPath) as! CustomCell
let ClientName = invoiceList[indexPath.row]
cell.ClientNameLabel.text = ClientName
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.