2016-03-20 40 views
0

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

+0

你能展示你的班级的完整样本吗?通常导致你的代码应该工作 - 除了示例函数应该像'func example(){...}'... –

+1

你真的写imagePicked == 2而不是imagePicked = 2? – FredericP

+0

在您的'profilbildtapped'函数中放置一个'print'输出,以查看它是否真的在您的图像视图中被调用... –

回答

2

好的。问题出在您的titelbildtapped/profilbildtapped函数中。有你有与单个=代替双==它检查是否相等分配值/。

so change imagePicked == 1/imagePicked == 2 to imagePicked = 1/imagePicked = 2在这些功能,它应该工作!

+0

不用担心。很高兴我能帮上忙。 :) –