如果我有空的 textField datePicker 在detailTextLabel 中传递数据,如何禁用“完成”按钮

发布于 2025-01-11 18:42:27 字数 794 浏览 0 评论 0原文

我有两个视图控制器。第一个是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚我心虐我身 2025-01-18 18:42:27

看起来您正在尝试构建的是一个待办事项列表应用程序,它可以防止用户输入没有标题的任务项。

您的第一个视图控制器 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 中的内容:

import UIKit

class AddAndEditVC: UIViewController {
    @IBOutlet weak var taskNameField: UITextField!
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var doneButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        doneButton.isEnabled = false
        taskNameField.delegate = self
    }

    @IBAction func addNewTask(_ sender: UIButton) {
        self.performSegue(withIdentifier: "backToTasksList", sender: self)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let newItem = Item(title: taskNameField.text!, date: datePicker.date)
        let destinationVC = segue.destination as! MyListOfTasksVC
        destinationVC.newItem = newItem
    }
}

extension AddAndEditVC: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.endEditing(true)
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField.text != nil && textField.text != "" {
            doneButton.isEnabled = true
        } else {
            doneButton.isEnabled = false
        }
    }
}

这是 MyListOfTasksVC

import UIKit

struct Item {
    var title: String
    var date: Date
}

class MyListOfTasksVC: UIViewController {
    @IBOutlet weak var tasksTable: UITableView!
    
    var newItem: Item?
    
    var items: [Item] = [Item(title: "Wake Up", date: Date()), Item(title: "Feed Dog", date: Date()), Item(title: "Finish Report", date: Date())]

    override func viewDidLoad() {
        super.viewDidLoad()
        tasksTable.dataSource = self
        
        if newItem != nil {
            items.append(newItem!)
        }
    }

    @IBAction func toAddAndEditScreen(_ sender: UIButton) {
        self.performSegue(withIdentifier: "toAddAndEdit", sender: self)
    }
}

extension MyListOfTasksVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    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)
        
        cell.textLabel?.text = item.title
        
        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
    }
}

这是您正在寻找的功能吗?

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 a UITextField for entering a new task's title and a UIDatePicker to set a deadline for the task. Somewhere on the second view controller, there is a 'DONE' button that creates an Item struct with the text from the UITextField as the .title property and the date from the UIDatePicker as the .date property. This struct then gets passed back to MyListOfTasksVC and appended to the array items, and is displayed as another task in the UITableView.

The question you're asking seems to be, "How to disable this button if the value of textField.text is nil or the empty string?"

The simplest way I know of is to set the 'DONE' button's isEnabled property inside your UITextFieldDelegate method textFieldDidEndEditing. If the value of your text field's text is nil or "", set doneButton.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:

import UIKit

class AddAndEditVC: UIViewController {
    @IBOutlet weak var taskNameField: UITextField!
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var doneButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        doneButton.isEnabled = false
        taskNameField.delegate = self
    }

    @IBAction func addNewTask(_ sender: UIButton) {
        self.performSegue(withIdentifier: "backToTasksList", sender: self)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let newItem = Item(title: taskNameField.text!, date: datePicker.date)
        let destinationVC = segue.destination as! MyListOfTasksVC
        destinationVC.newItem = newItem
    }
}

extension AddAndEditVC: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.endEditing(true)
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField.text != nil && textField.text != "" {
            doneButton.isEnabled = true
        } else {
            doneButton.isEnabled = false
        }
    }
}

and here is MyListOfTasksVC:

import UIKit

struct Item {
    var title: String
    var date: Date
}

class MyListOfTasksVC: UIViewController {
    @IBOutlet weak var tasksTable: UITableView!
    
    var newItem: Item?
    
    var items: [Item] = [Item(title: "Wake Up", date: Date()), Item(title: "Feed Dog", date: Date()), Item(title: "Finish Report", date: Date())]

    override func viewDidLoad() {
        super.viewDidLoad()
        tasksTable.dataSource = self
        
        if newItem != nil {
            items.append(newItem!)
        }
    }

    @IBAction func toAddAndEditScreen(_ sender: UIButton) {
        self.performSegue(withIdentifier: "toAddAndEdit", sender: self)
    }
}

extension MyListOfTasksVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    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)
        
        cell.textLabel?.text = item.title
        
        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
    }
}

Is this the functionality you're looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文