【发布时间】:2017-01-09 20:27:48
【问题描述】:
新手编码器,我被困住了。我的 Viewcontroller 上有 2 个UITextField,我想在点击“保存”时将其传递给UITableView。我已经将两个 textFields 都设置为 delegate.self,问题是如果我删除了 UItextFields 中的 1 个,那么在 textfields 中输入的数据只会显示在 UITableView 上。
我正在考虑为了在同一个 VC 上使用 2 个(或更多) UITextFields,除了插座之外,我必须需要一种方法来区分它们。我看过关于标签的回复,但我不明白。
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)
}
}
【问题讨论】:
-
您能否更好地解释一下问题所在。发送第二个文本字段时它如何不起作用?它崩溃了吗?发送的是空字符串吗?
-
我的故事板有 2 个文本字段和一个保存按钮。正如所写,应用程序不会将在任一文本字段中输入的任何数据添加到表格视图中。但是,一旦我删除了一个及其所有组件,它就可以工作了。因此,我假设您不能在不分配某种顺序或优先级的情况下将多个文本字段分配为同一视图控制器的委托。 @boidkan
-
我可以删除任何一个文本字段,并且在剩余文本字段中输入的任何内容都将传递给表格视图,没有我缺少的东西就不可能有 2 个文本字段。 @boidkan
-
因此您可以拥有多个具有相同委托的文本字段,您只需在委托方法中处理它即可。您可以通过检查传递给方法的文本字段来做到这一点,如下所示:
func textFieldDidEndEditing(textField: UITextField) { if textField === nameTextField{ } }三元组===检查它们是否是完全相同的对象。 -
我建议尝试通过在 segueing 之前检查文本字段的值来进行调试。你在某个地方有一个错误,如果没有我们面前的项目,我们很难确切知道它在哪里。
标签: iphone uitableview tags uitextfield