简体   繁体   中英

how do I pass my button image to an Image view when its clicked?

I think this is a very straight forward process but I'm not clear on the correct syntax for this but here's what I've done so far:

@IBOutlet weak var notesImage: UIImageView!

@IBAction func MusicNote(sender: AnyObject) {
    self.sendButtonImage()

}

// This is where the button image is sent to the UIImageView once that specific button is pressed
func sendButtonImage() {
    var note = MusicNote(sender: AnyObject)

Here is some rough code, you might need to unwrap some optionals, but the theory should work. I would personally set the images on the buttons in your viewDidAppear method or something that is done programmatically. Then use the method backgroundImageForState to retrieve the image again. Look at the documentation for UIButton here .

//Set this in view did appear, ect..
var firstImage = UIImage(named: "image1")
var secondImage = UIImage(named: "image2")
var thirdImage = UIImage(named: "image3")

button1.setBackgroundImage(firstImage!, forState state: UIControlState.SomeState) 
button2.setBackgroundImage(secondImage!, forState state: UIControlState.SomeState) 
button3.setBackgroundImage(thirdImage!, forState state: UIControlState.SomeState) 
//End code that should be in viewDidAppear

func buttonTapped(button1: UIButton?){
  imageView.image = button1.backgroundImageForState(UIControlState.SomeState)!
}

Let me know if you need any more help, and don't forget to upvote if you found my post useful, and accept if it helped you solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM