简体   繁体   English

快速将文本字段传递到第二个视图控制器中的标签

[英]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.

相关问题 Swift:将注释标题传递给第二个视图控制器标签 - Swift: Pass annotation title to my second view controller label 如何将数据源传递给第二个View Controller? 迅速 - How to pass datasource to second View Controller? Swift 在第一个视图控制器上的标签中显示输入到文本字段(在第二个视图控制器中)的文本? - Displaying text entered into textfield (in second view controller) in a label on first view controller? iOS:如何将表视图单元格的第一个视图控制器标签数据传递给第二个视图控制器的textView? - iOS: How to pass first view controller label data of table view cell to textView of second view controller? 如何在swift的第二个视图控制器中将信息传递给Uilabel? - How do i pass information to a Uilabel in the second view controller in swift? 如何将相机拍摄的图像传递到第二视图 controller? Swift - How to pass image captured with camera to second view controller? Swift 将字符串传递给第二个视图控制器 - Pass a string to a second view controller 如何通过 Swift UI 中的按钮单击将 Textfield 值传递给视图控制器 - How to pass Textfield value to view controller through button click in Swift UI 如何在同一视图控制器上选择文本字段(Swift 3) - how to segue a textfield on the same view controller (swift 3) 在Swift中从文本字段中选择使用表视图的视图控制器时出现错误 - Error when segueing to view controller with Table View from Textfield in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM