gpt4 book ai didi

ios - 如何将颜色保存到 Realm 并在其他 View Controller 中显示颜色?

转载 作者:行者123 更新时间:2023-11-29 05:21:48 25 4
gpt4 key购买 nike

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

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

我怎样才能做到这一点?

类别形式:-

import RealmSwift

class CategoryForm : Object {


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

}

类别保存颜色:-

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)")

}
}

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

显示 View Controller :-

 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 并在其他 View Controller 中显示颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58606517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com