user3266053
user3266053

Reputation: 175

Use a variable to change UIButton image

I want to change a UIButton image using a variable that contains the name of the image. The variable name is cardSeaTapped and contain "ruterEss".

I think of something like this (I know this is wrong):

btnCardSea1.setImage(UIImage(cardSeaTapped, ofType: "png"), for: .normal)

Upvotes: 0

Views: 164

Answers (2)

Rajan Singh
Rajan Singh

Reputation: 202

You can use image literal You have to drag and drop your image into assets and then in setImage method you have to write image literal and then a image logo will appear and when you double tap on it then you will get all your images and then you have to choose one no need to write the image name in your code. when you comment your code then it will looks like //btnCardSea1.setImage(#imageLiteral(resourceName: "cardSeaTapped"), for: .normal)

plz check image

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100523

You may need

btnCardSea1.setImage(UIImage(named:"\(cardSeaTapped).png"), for: .normal)

or

btnCardSea1.setImage(UIImage(named:cardSeaTapped), for: .normal)

where cardSeaTapped is of String type

Upvotes: 2

Related Questions