在Xcode 11.4中,我无法隐藏按钮

时间:2020-05-26 11:07:44

标签: swift xcode

因此,我正在尝试制作一个绘画应用程序,该应用程序可以存储用户提供的数据(例如绘画名称,绘画艺术家,绘画年份和绘画图像)并以表格视图显示。我有2个视图控制器,第一个称为ViewController,它具有用于显示数据的表视图(仅表视图单元格的绘画名称),第二个称为DetailedVC,用于输入并保存数据,还显示数据的详细信息。在第二个视图控制器中,我添加了3个文本字段,一个使用户可以进入照片库的图像视图和一个保存按钮。我在DetailedVC脚本中写了这段代码,以供用户在ViewController中点击表视图中的元素之一时使用:

override func viewDidLoad() {
        super.viewDidLoad()
        if chosenPainting != "" {

            savebutton.isHidden = true //Trying to hide the save button here!!

            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            let context = appDelegate.persistentContainer.viewContext

            let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Paintings")
            let idString = chosenPaintingId?.uuidString
            fetch.predicate = NSPredicate(format: "id = %@", idString!)
            fetch.returnsObjectsAsFaults = false

            do {
                let results = try context.fetch(fetch)
                if results.count > 0 {
                    for result in results as! [NSManagedObject] {
                        if let name = result.value(forKey: "name") as? String {
                            nameText.text = name
                        }
                        if let artist = result.value(forKey: "artist") as? String {
                            artistText.text = artist
                        }
                        if let year = result.value(forKey: "year") as? Int {
                            yearText.text = String(year)
                        }
                        if let imageData = result.value(forKey: "image") as? Data {
                            let imageD = UIImage(data: imageData)
                            imageView.image = imageD
                        }
                    }
                }
            } catch {
                print("error")
            }
        } else {
            savebutton.isEnabled = false
            nameText.text = ""
            artistText.text = ""
            yearText.text = ""
        }

我这样从我的ViewController获取信息:

    @objc func AddButtonTapped(){
        selectedPainting = ""
        performSegue(withIdentifier: "toDetailedVC", sender: self)
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedPaintingId = ids[indexPath.row]
        selectedPainting = names[indexPath.row]
        performSegue(withIdentifier: "toDetailedVC", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toDetailedVC" {
            let destination = segue.destination as! DetailedVC
            destination.chosenPainting = selectedPainting
            destination.chosenPaintingId = selectedPaintingId
        }
    }

它在savebutton.isHidden = true以下可以正常工作。我不明白为什么按钮没有消失。其他所有内容均正常运行,文本字段的文本转变为用户提供的信息,但按钮仍位于底部的右侧。甚至savebutton.isEnabled = false都在工作。我以为脚本和情节提要之间的连接必定有问题,因此我删除并重新进行了操作,但此操作不再起作用。我一定做错了什么,您能帮我吗?

0 个答案:

没有答案