gpt4 book ai didi

ios - 在函数外更改 var 的值

转载 作者:行者123 更新时间:2023-11-28 12:52:21 25 4
gpt4 key购买 nike

我在类(class)顶部声明了 var imagePicked = 0。现在,当我像这样在 IBAction 中更改 imagePicked 的值时:

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。我如何更改它的值,以便它不仅在我更改它的函数内部发生变化?

最佳答案

好的。问题出在您的 titelbildtapped/profilbildtapped 函数中。在那里你必须分配值 1/2 用一个 = 而不是双 == 来检查平等。

所以将 imagePicked == 1/imagePicked == 2 更改为 imagePicked = 1/imagePicked = 2在这些功能中,它应该可以工作!

关于ios - 在函数外更改 var 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36113706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com