2016-03-29 5 views
0

私はXcode(swift)には本当に新しく、UIImageViewを3つの画像の1つをランダムに表示させようとしていますが、シミュレータの2番目のviewController何も起こらず、XCodeはViewDidLoad関数の終わりをThread 1:breakpoint 1.1と強調表示します。UIImageViewの画像を変更する、他のXcodeの問題

UIImageViewで画像を切り替える方法がより信頼性が高く、より信頼性の高い方法があれば、間違いなく知りたいと思います。

私の現在のコード:

import UIKit 

let imageInitialIdentity = arc4random_uniform(3) 

class SecondViewController: UIViewController { 



    override func viewDidLoad() { 
     super.viewDidLoad() 



     if(imageInitialIdentity == 0){ 
      imageGuess.image = UIImage(named: "0ps.pngs") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
     } 
     if(imageInitialIdentity == 1){ 
      imageGuess.image = UIImage(named: "250ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
     } 
     if(imageInitialIdentity == 2){ 
      imageGuess.image = UIImage(named: "500ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
     } 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 

    } 


    @IBOutlet weak var imageGuess: UIImageView! 

    var imageIdentity = imageInitialIdentity 
    var score = 0 
    let imageZero = UIImage.init(named: "0ps") 
    let image250 = UIImage.init(named: "250ps") 
    let image500 = UIImage.init(named: "500ps") 





    @IBAction func guessZero(sender: UIButton) { 

     //if zero matches the picture 0, add a point 
     if imageIdentity == 0{ 
      score++ 
     } 
     else{ 
      //print incorrect, corect answer is 0 
     } 

      //randomizes another picture 
      let rand1 = arc4random_uniform(3) 
      if rand1 == 0 { 
       imageGuess.image = UIImage(named: "0ps.png") 
       imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
       imageIdentity = 0 
      } 
      else if rand1 == 1{ 
       imageGuess.image = UIImage(named:"250ps.png") 
       imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
       imageIdentity = 1 
      } 
      else if rand1 == 2{ 
       imageGuess.image = UIImage(named:"500ps.png") 
       imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
       imageIdentity = 2 
      } 

    } 

    @IBAction func guess250(sender: UIButton) { 
     // if 150 matches the picture of 250, return true 
     //randomizes another picture 
     //adds one to score? 
     if imageIdentity == 1{ 
      score++ 
     } 


     let rand2 = arc4random_uniform(3) 
     if rand2 == 0 { 
      imageGuess.image = UIImage(named:"0ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
      imageIdentity = 0 
     } 
     else if rand2 == 1{ 
      imageGuess.image = UIImage(named:"250ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
      imageIdentity = 1 

     } 
     else if rand2 == 2{ 
      imageGuess.image = UIImage(named:"500ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
      imageIdentity = 2 
     } 
    } 

    @IBAction func guess500(sender: UIButton) { 
     //if 500 matches the picture of 500, return true 
     //randomizes another picture 
     if imageIdentity == 2{ 
      score++ 
     } 


     let rand3 = arc4random_uniform(3) 
     if rand3 == 0 { 
      imageGuess.image = UIImage(named:"0ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
      imageIdentity = 0 
     } 
     else if rand3 == 1{ 
      imageGuess.image = UIImage(named:"250ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
      imageIdentity = 1 

     } 
     else if rand3 == 2{ 
      imageGuess.image = UIImage(named:"500ps.png") 
      imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340) 
      imageIdentity = 2 
     } 
    } 

} 
+1

あなたにはブレークポイントが設定されているようです。ブレークポイントを無効または削除します。 – ryantxr

+0

ブレークポイントをオフにする方法は、http://stackoverflow.com/questions/25750595/error-thread-1-breakpoint-1-1の指示に従うことができます。 – beyowulf

答えて

0

ブレークポイントを設定しているように見えます。通常、エラーを表示するためにクリックしたときに偶発的に発生します。コードが1つのViewControllerからこのSecondViewControllerに実行している、とのviewDidLoadでブレークした場合、あなたはどうするかをチェックすることをお勧めします

enter image description here

0

:あなたはボタンのような青色の矢印をクリックした場合には、ブレークポイントを無効にしますコードは前のクラスで終了し、SecondViewControllerクラスを実行するものは何ですか。これを確認したら、ランダムなメソッドをviewDidLoadから移動して、クラッシュの原因を突き止めることができます。以下のような何か:「UIImageViewは、画像を切り替えるために取得するためのより良いまたはより信頼性の高い方法がある場合は、」

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    randomMethod() 
} 
func randomMethod() // put a break point here if viewDidLoad passes 
{ 
    if(imageInitialIdentity == 0){ 
    ..... your code .... 
    } 
} 
0

は、あなたの質問に答えるために。

短い答え、いいえ、それはやり方です。 注意すべき点は、変更を確認するためにメインスレッドにイメージを設定する必要があることです。 viewDidLoadメソッドはメインスレッド上で実行されているので、ここで心配はありません。 イメージビューのイメージを後で更新する場合は、GCDを使用してUIを正しく更新できます。

dispatch_async(dispatch_get_main_queue(), ^{ 
    // update your UI here 
}); 

イメージビューで2つのイメージをすばやく切り替える必要がある場合は、highlightedImageプロパティを使用できます。 このプロパティは、イメージプロパティの場合と同じように設定します(もちろん、イメージは異なります)。 次に、強調表示されたプロパティをYESまたはNOに設定するだけです。

UIImageView *iv = [UIImageView new]; 
UIImage *firstImage = [UIImage imageNamed:@"first_image.png"]; 
iv.image = firstImage; 
iv.highlightedImage = [UIImage imageNamed:@"second_image.png"]; 
// Call below code whenever you want to switch the image, by setting the boolean. 
dispatch_async(dispatch_get_main_queue(), ^{ 
    iv.highlighted = YES; 
}); 

イメージビューのフレームにイメージサイズプロパティを使用することをお勧めします。

iv.frame = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height); 
関連する問題