我需要存储颜色并在其他viewController中显示颜色。当我保存时,像这样保存UIDynamicSystemColor:0x600001127620;名称= systemBlueColor

有了这些数据,我如何显示颜色,它是领域对象中的字符串类型。

我怎样才能做到这一点?

CategoryForm:-

import RealmSwift

class CategoryForm : Object {


    @objc dynamic var categoryName : String = ""
    @objc dynamic var categoryColor : String = ""

}


类别SaveColor:-

struct CategoryVaraible {

    static  var categoryArray : Results<CategoryForm>?

}
class CategoryDetailsViewController : UIViewController {

var categories = CategoryVaraible.categoryArray

    var color = [UIColor.secondarySystemFill,UIColor.systemRed,UIColor.systemPink,UIColor.systemOrange,UIColor.systemYellow,UIColor.systemGreen,UIColor.systemBlue,UIColor.systemPurple]

     var colorSelected : UIColor!

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
             let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "ColorViewCell", for: indexPath) as! ColorViewCell
            cell.backgroundColor = color[indexPath.row]


            return cell

           }
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


            let item = indexPath.row
            if item == 0 {
                colorSelected = UIColor.secondarySystemFill
            }else if item == 1 {
                colorSelected = UIColor.systemRed
            }else if item == 2 {
                colorSelected = UIColor.systemPink
            }else if item == 3 {
                colorSelected = UIColor.systemOrange
            }else if item == 4 {
                colorSelected = UIColor.systemYellow
            }else if item == 5 {
                colorSelected = UIColor.systemGreen
            }else if item == 6 {
                colorSelected = UIColor.systemBlue
            }else if item == 7 {
                colorSelected = UIColor.systemPurple
            }
          }
    //Here Saving
     @IBAction func saveAction(_ sender: UIButton) {
             let newArray = CategoryForm()
     newArray.categoryName = categoryName.text!

           newArray.categoryColor = "\(colorSelected!)"

     self.saveItems(category: newArray)

      self.navigationController?.popViewController(animated: true)

    }

    func saveItems(category: CategoryForm) {
                                       do{

                                           try realm.write {//Save/Create
                                               realm.add(category)
                                           }

                                       }catch {
                                           print("Error in saving \(error)")

                                       }
                                   }


保存后在这里显示保存的颜色

显示ViewController:-

 var categories = CategoryVaraible.categoryArray

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return categories?.count ?? 1

           }

           func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
               let cell = self.tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) as! CategoryCell

            let colorStr = categories?[indexPath.row].categoryColor

<UIDynamicSystemColor: 0x600001127620; name = systemBlueColor>

            cell.circleImage.backgroundColor =   UIImage(name: colorStr as! String)//Here We need to display color
    return cell
    }

最佳答案

如果您不关心暗模式和系统颜色,我将添加几个简单的扩展,以将UIColor序列化/反序列化为标准值(例如json中的ARGB或更好的Int十六进制)。

多种解决方案,例如:https://stackoverflow.com/a/19072934/691977

然后只存储单个String或Int值。
读取时,有一个访问器方法可将存储的值转换为UIInt。

就像是:

extension UIColor {
   init(hex: UInt32){
       ...
   }
   var hex: UInt32 {
       ...
   }
}


class CategoryForm : Object {


    @objc dynamic var categoryName : String = ""
    @objc dynamic var categoryColor : UInt32 = 0

    var color: UIColor? {
        get {
            return UIColor(hex: self.categoryColor)
        }
        set {
            self.categoryColor = newValue.hex
        }
    }



}

关于ios - 如何将颜色保存到 Realm 并在其他viewController中显示颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58606517/

10-11 22:14
查看更多