我有一個UIImagePickerController和2個按鈕。當我點擊按鈕1時,我想設置按鈕1的圖像。當我點擊按鈕2時,我想設置按鈕2的圖像。我已經使用UIImagePickerController成功設置了按鈕1的圖像,但未能如此與按鈕2.這是我的代碼:1 UIImagePickerController和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,你可以進一步瞭解我如何檢查,在調試器?謝謝 –
如果您不知道如何使用調試器,請使用print語句。要了解如何使用Xcode,請在線查找教程或訪問developer.apple.com。 – Mundi
好吧,所以我檢查和功能正確連接,任何想法爲什麼代碼仍然不工作? –