UIButton背景图像未从Singleton字典

问题描述:

中再次设置。UIButton背景图像未从Singleton字典

我刚刚完成一个字典的单实例用下面的代码 在文件Recipes.swift

static let sharedInstance = Recipes() 

var imagesOfChef = [Int : chefImages]() 
struct chefImages { 
     var objidChef: String! 
     var imageChef: UIImage! 
    } 

而当过程结束后我有这样的代码

let singleton = Recipes.sharedInstance 
singleton.imagesOfChef = self.imagesOfChef 

所以singletonimagesOfChef具有相同的字典。

在下一个视图中,我想访问Dictionary的所有数据,并使用Dictionary所具有的图像之一设置按钮的背景图像。 所以在cellForItemAtIndexPath我有以下代码

let test = userPointer.objectId 
    let otherReferenceToTheSameSingleton = Recipes.sharedInstance 




for i in 0...otherReferenceToTheSameSingleton.imagesOfChef.count - 1 { 
    print(otherReferenceToTheSameSingleton.imagesOfChef[i]!.objidChef) //it prints the object id's of the Chef normally 

    if otherReferenceToTheSameSingleton.imagesOfChef[i]!.objidChef == test! { 
     print("done") 
     print(otherReferenceToTheSameSingleton.imagesOfChef[i]!.imageChef) 
     cell.avatarOutlet.setImage(otherReferenceToTheSameSingleton.imagesOfChef[i]!.imageChef, forState: .Normal) //but it doesnt set the Image as a background 

    } 

} 

所以当厨师的对象ID的对象的ID是词典,我想设置为背景是同一行中的图像内匹配。

但它不!

虽然当if statement是正确的,如果你看到我尝试打印图像,我也得到

<UIImage: 0x1582c9b50>, {60, 60} 

怎么可能设置此图片作为背景?!?!

非常感谢!

+0

按钮是否以编程方式创建? – Sofeda

设置按钮类型的自定义如果编程创建

let button = UIButton(type: UIButtonType.Custom) as UIButton 

如果是从Stroyboard更改类型存在。

+0

哦..是的,这是解决方案! –