【问题标题】:UIButton Background image is not set from Singleton DictionaryUIButton 背景图像未从 Singleton Dictionary 设置
【发布时间】:2016-08-13 13:48:15
【问题描述】:

再次。

我刚刚使用以下代码完成了字典的单例示例 在文件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

    }

}

因此,当 Chef 的对象 id 与 Dictionary 中的对象 id 匹配时,我想将同一行中的图像设置为背景。

但事实并非如此!

虽然当if statement 正确时,如果你看到我尝试打印图像并且我得到了

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

怎么可能把这张图片设置为背景??!?!

非常感谢!

【问题讨论】:

  • 您的按钮是否以编程方式创建?

标签: ios swift swift2 ios9 swift-dictionary


【解决方案1】:

设置自定义按钮类型(如果它是通过编程方式创建的)

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

如果它来自 Stroyboard,请更改那里的类型。

【讨论】:

  • 哦..是的,这就是解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多