管理多个UItextField

时间:2016-09-01 20:19:37

标签: iphone uitableview tags uitextfield

新手编码器,我被卡住了。我的Viewcontroller上有2 UITextField,当我点击“保存”时,我希望将其传递给UITableView。我已经将textFields设置为delegate.self,问题是在textfields中输入的数据只显示在UITableView上我删除了1个UItextFields。

我想在同一个VC上使用2个(或更多个)UITextField我必须除了出口之外还需要一种方法来区分它们。我看过有关标签的回复,但我不明白。

import UIKit

class BudgetViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate {

    // Properties:
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var dateDisplay: UILabel!
    @IBOutlet weak var saveButton: UIBarButtonItem!


    var budget: Budget?

    // Date picker:
    let dateFormatter = NSDateFormatter()

    func setDate() {
        dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
        dateDisplay.text = dateFormatter.stringFromDate(datePicker.date)
    }

    // Navigation

    // This method lets you configure a view controller before it's presented
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if saveButton === sender {

            let name = nameTextField.text ?? ""
            let date = dateDisplay.text ?? ""
            let amount = amountTextField.text ?? ""

    // set the budget to be passed to the Controller, this code configures the meal prperty with the appropriate values before the segue executes

            budget = Budget(date: date, name: name, amount: amount)     
        }
    }

    // Actions:
    @IBAction func datePickerChanger(sender: AnyObject) {
        setDate()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Handle the text field

        nameTextField.delegate = self
        amountTextField.delegate = self
    }
    // UITextFieldDelegate

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(textField: UITextField) {


    }
}




import UIKit

class BudgetTableViewController: UITableViewController {

    //Properties
    var budgets = [Budget]()


    override func viewDidLoad() {
        super.viewDidLoad()
        loadSampleBudgets()

    }

    func loadSampleBudgets() {

        let budget1 = Budget(date: "8/16/2016", name: "Eyebrows", amount: "15")!
        let budget2 = Budget(date: "8/28/2016", name: "Acme", amount: "59")!
        let budget3 = Budget(date: "9/10/2016", name: "Wildwood", amount: "199")!

        budgets += [budget1, budget2, budget3]
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection    section: Int) -> Int {

        return budgets.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "BudgetTableViewCell"

        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,     forIndexPath: indexPath) as! BudgetTableViewCell
        let budget = budgets[indexPath.row]

        cell.dateLabel.text = budget.date
        cell.nameLabel.text = budget.name
        cell.amountLabel.text = budget.amount
        return cell
    }

@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? BudgetViewController, budget = sourceViewController.budget {

//Add a new meal

    let newIndexPath = NSIndexPath(forRow: budgets.count, inSection: 0)
    budgets.append(budget)
    tableView.insertRowsAtIndexPaths([indexPath], withRowanimation: .Bottom)
        }
    }

1 个答案:

答案 0 :(得分:0)

检查您的文字字段是否与故事板正确链接。每个IBOutlet旁边应该有一个填充圆圈。如果您在XCode中打开了代码和故事板,并将鼠标悬停在每个插座旁边的圆圈上,则文本字段应在故事板上突出显示。

如果您复制并粘贴了原始名称文本字段并将其更改为金额文本字段,则它实际上仍可能引用名称插座。您可以通过右键单击故事板中的文本字段来查看它,并查看它引用的出口。

    override func viewDidLoad() {
    super.viewDidLoad()

        nameTextField.delegate = self
        streetTextField.delegate = self
        cityTextField.delegate = self
        stateTextField.delegate = self
        countryTextField.delegate = self
        phoneTextField.delegate = self
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if textField == nameTextField {
            streetTextField.becomeFirstResponder()
        }
        else if textField == streetTextField {
            cityTextField.becomeFirstResponder()
        }
        else if textField == cityTextField {
            stateTextField.becomeFirstResponder()
        }
        else if textField == stateTextField {
            countryTextField.becomeFirstResponder()
        }
        else if textField == countryTextField {
            phoneTextField.becomeFirstResponder()
        }
        else if textField == phoneTextField {
            nameTextField.becomeFirstResponder()
        }
        return true
    }

这是我编写的一些代码,允许用户填写表单(按回车键从一个字段移动到下一个字段),所以你肯定可以拥有多个具有相同委托的textFields。