私はクラスの先頭にvar imagePicked = 0
と宣言しました。 今私はここのようにIBAction内部imagePickedの値を変更します。関数外のvarの値を変更する
import UIKit
クラスのViewController:imagePickedののUIViewController、UIImagePickerControllerDelegate、UINavigationControllerDelegate {
@IBOutlet weak var titelbild: UIButton!
@IBOutlet weak var profilbild: UIButton!
let imagePicker = UIImagePickerController()
var imagePicked = 0
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
// imagePicker.allowsEditing = false
// imagePicker.sourceType = .PhotoLibrary
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func titelbildtapped(sender: AnyObject) {
// if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
imagePicked == 1
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
// }
}
@IBAction func profilbildtapped(sender: AnyObject) {
imagePicked == 2
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
print ("output")
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if imagePicked == 1 {
titelbild.setImage(pickedImage, forState: UIControlState.Normal)
// titelbild.imageView?.image = pickedImage
} else if imagePicked == 2 {
profilbild.setImage(pickedImage, forState: .Normal) }
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
値があるように思われます私はそれの値を変更することができますので、私はそれを変更した関数内で変更されていません?
あなたはクラスの完全なサンプルを表示できますか?あなたのコードは正常に動作するはずです - 例の関数は 'func example(){...}'のようなものでなければなりません... –
imagePicked = 2ならば、本当にimagePicked == 2を書きましたか? – FredericP
'print'出力を' profilbildtapped'関数に入れて画像ビューアをタップすると本当に呼び出されるかどうかを確認します。 –