在函数外更改 var 的值
Change value of var outside the function
我在 class 的顶部声明了 var imagePicked = 0
。
现在,当我在 IBAction 中更改 imagePicked 的值时,如下所示:
import UIKit
class 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。我怎样才能改变它的值,所以它不仅在我改变它的函数内部改变了?
好的。问题出在您的 titelbildtapped
/ profilbildtapped
函数中。在那里你必须分配值 1 / 2 与单个 = 而不是双 == 检查是否相等。
所以在这些函数中将 imagePicked == 1
/ imagePicked == 2
更改为 imagePicked = 1
/ imagePicked = 2
,它应该可以工作!
我在 class 的顶部声明了 var imagePicked = 0
。
现在,当我在 IBAction 中更改 imagePicked 的值时,如下所示:
import UIKit
class 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。我怎样才能改变它的值,所以它不仅在我改变它的函数内部改变了?
好的。问题出在您的 titelbildtapped
/ profilbildtapped
函数中。在那里你必须分配值 1 / 2 与单个 = 而不是双 == 检查是否相等。
所以在这些函数中将 imagePicked == 1
/ imagePicked == 2
更改为 imagePicked = 1
/ imagePicked = 2
,它应该可以工作!