我有一个 UIImagePickerController 和 2 个按钮。当我点击按钮 1 时,我想设置按钮 1 的图像。当我点击按钮 2 时,我想设置按钮 2 的图像。我已经使用 UIImagePickerController 成功设置了按钮 1 的图像,但未能成功使用按钮 2。这是我的代码:
var whichButton: Int = 0
func displayImagePicker(){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
@IBAction func chooseImageOne(sender: AnyObject) {
displayImagePicker()
whichButton = 1
}
@IBAction func chooseImageTwo(sender: AnyObject) {
displayImagePicker()
whichButton = 2
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
if whichButton == 1 {
self.imageOne.setImage(image, forState: UIControlState.Normal)
} else if whichButton == 2 {
self.imageTwo.setImage(image, forState: UIControlState.Normal)
}
}
正如你所看到的,我尝试使用一个 var 来跟踪我正在点击的按钮。我不确定我在 didFinishPickingImage 函数中所做的是否正确。我对 swift 很陌生,如果有人能阐明一些观点,那就太好了!提前致谢
最佳答案
我亲自在 xcode 上检查了你的代码,它是正确的。请查看您是否已将 IBOutlet 赋予 self.imageTwo 按钮,并检查 IBAction,即 imageTwo 按钮的 touchupInside。其他一切都很好
关于xcode - 1 个 UIImagePickerController 和 2 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34851820/