我宣佈var imagePicked = 0
位於我的課程頂部。 現在,當我改變imagePicked的價值IBAction爲內喜歡這裏:更改函數外的var值
import UIKit
類的ViewController: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)
}
}
imagePicked的價值似乎是仍然是0而不是2.我怎樣才能改變它的值,所以它不僅在函數內部改變了我改變了它?
你能展示你的班級的完整樣本嗎?通常導致你的代碼應該工作 - 除了示例函數應該像'func example(){...}'... –
你真的寫imagePicked == 2而不是imagePicked = 2? – FredericP
在您的'profilbildtapped'函數中放置一個'print'輸出,以查看它是否真的在您的圖像視圖中被調用... –