单击时如何将我的按钮图像传递到图像视图?

时间:2015-08-12 17:12:15

标签: ios swift button uiimage

我认为这是一个非常直接的过程,但我不清楚这个的正确语法,但这是我到目前为止所做的:

@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)

1 个答案:

答案 0 :(得分:0)

这是一些粗略的代码,您可能需要解开一些选项,但理论应该有效。我会亲自在viewDidAppear方法中的按钮上设置图像,或者以编程方式完成。然后使用方法backgroundImageForState再次检索图像。查看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)!
}

如果您需要更多帮助,请告诉我,如果您发现我的帖子有用,请不要忘记投票,并接受它是否有助于您解决问题。

相关问题