2016-03-20 2 views
0

私はクラスの先頭にvar imagePicked = 0と宣言しました。 今私はここのようにIBAction内部imagePickedの値を変更します。関数外のvarの値を変更する

import UIKit 

クラスのViewController:imagePickedのの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) 
} 

}

値があるように思われます私はそれの値を変更することができますので、私はそれを変更した関数内で変更されていません?

+0

あなたはクラスの完全なサンプルを表示できますか?あなたのコードは正常に動作するはずです - 例の関数は 'func example(){...}'のようなものでなければなりません... –

+1

imagePicked = 2ならば、本当にimagePicked == 2を書き​​ましたか? – FredericP

+0

'print'出力を' profilbildtapped'関数に入れて画像ビューアをタップすると本当に呼び出されるかどうかを確認します。 –

答えて

2

ok。あなたのtitelbildtapped/profilbildtapped機能に問題があります。そこではなく、平等をチェックダブル==の=単一と/値を割り当てる必要があります。

このように変更すると、imagePicked == 1/imagePicked == 2imagePicked = 1/imagePicked = 2のようになります。

+0

心配はありません。お役に立てて嬉しいです。 :) –

関連する問題