ios - 在同一个 View Controller 上按下 AlertViewController 上的确定按钮后重新加载 Collection View ?

标签 ios swift core-data uicollectionview uialertcontroller

如何在同一个 View Controller 上按下 AlertViewController 上的 OK 按钮后重新加载 Collection View ?

当我点击主屏幕然后再次返回此屏幕时,它会显示已保存的数据吗?当我按下 AlertController 上的 OK 按钮时,我想显示这些数据。我不知道如何解决这个问题。

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(true)

            //Fetch Category Data
            categoryCoreData.fetchData()
            self.collCategory.reloadData()

}


func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return addCategory.count + 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {


        let cellID = indexPath.row < addCategory.count ? "CategoryCell" : "ExtraCell"

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)

        setupCell(cell: cell, indexPath: indexPath, type: cellID)

        return cell
    }

    func setupCell(cell: UICollectionViewCell, indexPath: IndexPath, type: String)
    {
        switch(type)
        {
            case "CategoryCell":
                setupCategoryCell(cell: cell as! CategoryCollectionCell, indexPath: indexPath)
            case "ExtraCell":
                setupAddButtonCell(cell: cell as! CategoryExtraCell, indexPath: indexPath)
            default:
                break
        }
    }
func setupCategoryCell(cell: CategoryCollectionCell, indexPath: IndexPath)
    {
        let cat = addCategory[indexPath.row]
        cell.lblHeader.text = cat.category_name
    }

func setupAddButtonCell(cell: CategoryExtraCell, indexPath: IndexPath)
    {
        //Extra Button "Add Button" in a cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        if indexPath.item < addCategory.count
        {
            print("Main Cell")
        }
        else
        {
            print("Add New Cell")

            self.blurEffects()
            view.addSubview(blurEffectView)

            //Alert View Controller when Adding Categories...
            let inputBox = BMInputBox.boxWithStyle(.plainTextInput)
            inputBox.blurEffectStyle = .extraLight

            inputBox.title = NSLocalizedString("Add Category", comment: "")
            inputBox.message = NSLocalizedString("Please enter unique category name.", comment: "")

            inputBox.customiseInputElement = {(element: UITextField) in

                element.placeholder = "Enter a category"
                return element
            }

            inputBox.submitButtonText = NSLocalizedString("OK", comment: "")

            inputBox.onSubmit = {(value: String...) in

                //Store value in text field in "text" object.
                for text in value
                {
                    if text is String
                    {
                        //Store category in CoreData
                        categoryCoreData.saveData(tfCat: text)

                    }
                }
                self.blurEffectView.removeFromSuperview()

            }


            inputBox.cancelButtonText = NSLocalizedString("Cancel", comment: "")

            inputBox.onCancel = {
                //Remove blur effects from Superview
                self.blurEffectView.removeFromSuperview()
            }

            inputBox.show()
        }
    }

最佳答案

    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in

        self.collectionView.reloadData()

    }))
    self.present(alert, animated: true, completion: nil)

关于ios - 在同一个 View Controller 上按下 AlertViewController 上的确定按钮后重新加载 Collection View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48563227/

相关文章:

ios - MagicalRecord iCloud 问题

ios - 无法访问资源包

ios - iPhone X 上的警报通过 UIAlertController 弹出窗口显示不同的颜色

objective-c - 在虚拟键盘中单击“下一步”按钮期间从一个文本字段移动到另一个文本字段

ios - Alamofire - 将数据保存在存储的属性中

iphone - 核心数据 - 保存电子邮件地址

objective-c - 当我的应用程序在后台运行时,如何将项目与云同步?

iOS 使用 HTTPS 使用 WCF 服务

ios - Swift - 在 for 循环中创建具有不同名称的变量

swift - CoreML - 设备模型训练