0

I am trying to set a button image programmatically, and no one seems to have problems with this but me.

My Code:

func moreOptionsKeyMap(){
    for x in self.mainView.subviews as [UIButton]
    {
        let image = UIImage(named: "\(numberImages[0])") as UIImage
        if x.tag == 0 {
            x.setImage(UIImage (named: image), forState: .Normal)
        }

    }
}

I tried directly plugging in the name of the image (This is the line throwing the error)

x.setImage(UIImage (named: "1Key.png"), forState: .Normal)

but I keep getting the same error.

I found an answer on Stackoverflow that said the solution is:

let image = UIImage(named: "name") as UIImage
let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.setImage(image, forState: .Normal)

and everyone says that works fine, so I can't imagine what is wrong with mine.

I'm making a keyboard, so I have buttons set up in Interface Builder, and then in my viewDidLoad

for v in self.mainView.subviews as [UIButton]
    {
        v.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    }

then in the buttonPressed method I have a switch that calls moreOptionsKeyMap()

SomeGuy
  • 3,725
  • 3
  • 19
  • 23

1 Answers1

4

At a first glance I would say that here:

x.setImage(UIImage (named: image), forState: .Normal)

you are passing image, which is a UIImage, whereas a string is expected.

I think you have to turn this line:

let image = UIImage(named: "\(numberImages[0])") as UIImage

into:

let image = "\(numberImages[0])"

So this is how your method should look like:

func moreOptionsKeyMap() {
    for x in self.mainView.subviews as [UIButton]
    {
        let image = "\(numberImages[0])"
        if x.tag == 0 {
            x.setImage(UIImage (named: image), forState: .Normal)
        }
    }
}
Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Can you post your code? a gist is ok. I tested my modifications on a playground and it compiles – Antonio Oct 02 '14 at 20:19
  • That's pretty much all of the related code... check what I added, see if it's enough – SomeGuy Oct 02 '14 at 20:24
  • Ok I've updated my answer adding your method with my modifications. Please try it, it's compiling here, and with your original code I receive the same error. Also the error itself indicates that the problem is about using a `UIImage` where `NSString` is expected. – Antonio Oct 02 '14 at 20:29
  • Yeah, that's exactly how my code looks, and I get the same error... That's why I tried removing the possibility that numberImages had the wrong string or something and just directly typed a string into the code: x.setImage(UIImage (named: "1Key.png"), forState: .Normal) but I get the same error. – SomeGuy Oct 02 '14 at 20:38
  • Wow, the problem was Xcode. I cleaned the project and it compiled fine. Thanks for the help!! Appreciate it. – SomeGuy Oct 02 '14 at 20:40
  • Cool, glad it worked :) the swift compiler often provides unmeaningful error messages, but in this case it really seemed a nonsense – Antonio Oct 02 '14 at 20:42
  • I thought so too! I just tested it with the new code and compiled it, ran it, and my app crashed. Time to tackle the next problem..... :P – SomeGuy Oct 02 '14 at 20:44