2016-03-29 67 views
0

我真的很新,XCode(swift),我試圖讓一個UIImageView隨機顯示三個圖像之一,但是當我切換到模擬器中的第二個viewController ,什麼也沒有發生,並且XCode突出顯示了線程1:斷點1.1的ViewDidLoad函數的結束。更改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

它看起來就像你設置一個斷點。通常由於點擊查看錯誤而導致意外。如果代碼是從一個視圖控制器運行到這個SecondViewController

enter image description here

0

,並在viewDidLoad中程序中斷,你可能要檢查怎麼做的:如果你點擊喜歡按鈕的藍色箭頭,將停用破發點代碼完成了它的前一個類,並使其運行SecondViewController類。一旦你完成了檢查,你可能想要將你的隨機方法移出viewDidLoad來找出究竟是什麼導致了崩潰。如:

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

要回答你的問題,「如果有更好或更可靠的方式讓UIImageView切換圖像」。

簡短的回答,不,這是它的方式。 你應該注意的一件事是,你必須在主線程上設置圖像才能看到變化。 viewDidLoad方法當然是在主線程上運行,所以不用擔心。 如果您決定稍後更新imageview的圖片,則可以使用GCD來正確更新您的用戶界面。

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

如果您需要在imageview中的兩個圖像之間快速切換,可以使用highlightImage屬性。 您設置此屬性與您爲圖像屬性(當然不同的圖像)相同。然後,您只需將突出顯示的屬性設置爲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; 
}); 

我推薦的一件事是使用imageview的框架的圖像大小屬性。

iv.frame = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height); 
相關問題