如果我有空的 textField datePicker 在detailTextLabel 中传递数据,如何禁用“完成”按钮
我有两个视图控制器。第一个是 MyListOfTasksVC,第二个是 AddAndEditVC。在第二个 VC 中,我有一个日期选择器,并将日期传递给 cellForRowAt 中的字幕标签。在实现之前,如果文本字段为空,我的“完成”按钮将被禁用。但现在 datePicker 忽略了这一点并添加了仅包含日期的新行。
在这种情况下如何禁用“完成”按钮?我尝试过委托,但没有成功。
//in MyListOfTasksVC
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyDailyTasksCell", for: indexPath)
let item = items[indexPath.row]
configureText(for: cell, with: item)
configureCheckmark(for: cell, with: item)
let date = items[indexPath.row].date
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, h:mm a"
cell.detailTextLabel?.text = formatter.string(from: date)
return cell
}
I have two View Controller. The first one is MyListOfTasksVC and the second one is AddAndEditVC. In the second VC i have a date picker and pass date to subtitle lable in cellForRowAt. Before implementing that my Done button was disabled if textField is empty. But now datePicker ignores that and adds new row with just date.
How I can disable Done button in such situation? I tried to make delegate, but it wasn't successful.
//in MyListOfTasksVC
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyDailyTasksCell", for: indexPath)
let item = items[indexPath.row]
configureText(for: cell, with: item)
configureCheckmark(for: cell, with: item)
let date = items[indexPath.row].date
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, h:mm a"
cell.detailTextLabel?.text = formatter.string(from: date)
return cell
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试构建的是一个待办事项列表应用程序,它可以防止用户输入没有标题的任务项。
您的第一个视图控制器
MyListOfTasksVC
显示所有已添加的任务。您的第二个视图控制器
AddAndEditVC
有一个用于输入新任务标题的UITextField
和一个用于设置任务截止日期的UIDatePicker
。在第二个视图控制器上的某个位置,有一个“DONE”按钮,用于创建一个Item
结构,其中使用UITextField
中的文本作为.title
属性和来自UIDatePicker
的日期作为.date
属性。然后,该结构体被传回MyListOfTasksVC
并附加到数组items
中,并在UITableView
中显示为另一个任务。您问的问题似乎是“如果
textField.text
的值为nil
或空字符串,如何禁用此按钮?”我知道的最简单的方法是在
UITextFieldDelegate
方法textFieldDidEndEditing
中设置“DONE”按钮的isEnabled
属性。如果文本字段的文本值为nil
或""
,请设置doneButton.isEnabled = false
。否则,doneButton.isEnabled = true
。这样,该按钮仅允许提交具有标题的新任务。这是我在
AddAndEditVC
中的内容:这是
MyListOfTasksVC
:这是您正在寻找的功能吗?
It looks like what you're trying to build is a todo-list app that prevents the user from entering a task item without a title.
Your first view controller,
MyListOfTasksVC
, displays all the tasks which have already been added.Your second view controller,
AddAndEditVC
, has aUITextField
for entering a new task's title and aUIDatePicker
to set a deadline for the task. Somewhere on the second view controller, there is a 'DONE' button that creates anItem
struct with the text from theUITextField
as the.title
property and the date from theUIDatePicker
as the.date
property. This struct then gets passed back toMyListOfTasksVC
and appended to the arrayitems
, and is displayed as another task in theUITableView
.The question you're asking seems to be, "How to disable this button if the value of
textField.text
isnil
or the empty string?"The simplest way I know of is to set the 'DONE' button's
isEnabled
property inside yourUITextFieldDelegate
methodtextFieldDidEndEditing
. If the value of your text field's text isnil
or""
, setdoneButton.isEnabled = false
. Otherwise,doneButton.isEnabled = true
. This way, the button only allows submitting the new task if it has a title.Here is what I had inside
AddAndEditVC
:and here is
MyListOfTasksVC
:Is this the functionality you're looking for?