Search code examples
iosswiftswift2ios9swift-dictionary

UIButton Background image is not set from Singleton Dictionary


again.

I've just finished a singleton example of a Dictionary with the following code at the file Recipes.swift

 static let sharedInstance = Recipes()

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

And when a process is finished i have this code

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

So the singleton has the same dictionary as the imagesOfChef has.

On the next view i want to access all the data of the Dictionary and set a background image of the button with one of the images that the Dictionary has. So in the cellForItemAtIndexPath I have the following code

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

    }

}

So when the object id of the Chef matches the object id that is inside the Dictionary i want to set as background the image that is in the same row.

But it doesn't!

Although when the if statement is correct if you see i try to print the image and i get

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

How is it possible to set this image as background??!?!

Thanks a lot!


Solution

  • Set the Button type custom if it is created programmatically

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

    If it is from Stroyboard change the type there.