更改函数外的var值

时间:2016-03-20 12:30:05

标签: ios swift func

我宣布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.如何更改它的值,所以它不仅在我改变它的函数内部改变了?

1 个答案:

答案 0 :(得分:2)

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

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