2017-06-08 98 views
0

因此,經過研究,研究和外包我無法解決我的問題:(我目前正在嘗試習慣在傳遞值的時候繼續觀看視圖,所以最好開發一個簡單的應用程序呢?傳遞圖像並顯示它

我的應用程序是這樣的...與一個圖像陣列,它隨機吐出的圖像時的視圖載荷和一個動作buttonthat使用賽格瑞預製件功能觸發視圖SEGUE到下一視圖的第一頁。

的第二頁包含解散按鈕和圖像視圖,該視圖應該根據從預覽視圖圖像陣列隨機傳遞的圖像而改變。

一切工作正常,但我無法看到隨機傳遞的圖像。

下面是我的代碼:

1條

import UIKit 

class MainWelcomVC: UIViewController { 



var ImageArr = [UIImageView]() 

@IBOutlet weak var BackGround: UIImageView! 



override func viewDidLoad() { 
    super.viewDidLoad() 

    for x in 0...2 { 

     let image = UIImage(named: "char\(x).png") 
     let imageview = (UIImageView(image: image)) 
     ImageArr.append(imageview) 

    } 


} 

@IBAction func PlayButtonPressed(_ sender: Any) { 

    let randomIndex = Int(arc4random_uniform(UInt32(ImageArr.count))) 
    let CharImage = ImageArr[randomIndex] 

    performSegue(withIdentifier: "CharDisplayVC", sender: CharImage) 

} 



override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if let destination = segue.destination as? CharDisplayVC{ 

     if let ImageFinal = sender as? UIImage { 
      destination.ImageToDisplay = ImageFinal 
     } 
} 


} 
} 

第2頁

import UIKit 

class CharDisplayVC: UIViewController { 

private var _ImageToDisplay: UIImage! 

var ImageToDisplay: UIImage { 

    get { 

     return _ImageToDisplay 

    } set { 

     _ImageToDisplay = newValue 
    } 

} 


@IBOutlet weak var CharPicture: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    CharPicture.image = _ImageToDisplay 


} 

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

@IBAction func PlayAgainBtnPressed(_ sender: Any) { 

    dismiss(animated: true, completion: nil) 
} 



} 

回答

0

於是終於找到了解決辦法鬼混後,它是使一切UIImage對象包括陣列,以下是第1頁的新代碼:

import UIKit 

class MainWelcomVC: UIViewController { 

var ImageArr = [UIImage]() 
var CharImage: UIImage? 

@IBOutlet weak var BackGround: UIImageView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    for x in 0...2 { 

     let image = UIImage(named: "char\(x).png") 
     ImageArr.append(image!) 
    } 
} 




@IBAction func PlayButtonPressed(_ sender: Any) { 

    let randomIndex = Int(arc4random_uniform(UInt32(ImageArr.count))) 
    self.CharImage = ImageArr[randomIndex] 

    performSegue(withIdentifier: "CharDisplayVC", sender: self) 
} 



override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if let destination = segue.destination as? CharDisplayVC { 

     if let ImageFinal = self.CharImage as? UIImage! { 
      destination.ImageToDisplay = ImageFinal 
     } 
    } 
    } 
} 

我仍然有一個黃色的警告其中說「從'UIImage有條件的演員?' 'UIImage!'總是成功「,另一個說」模式匹配「引入了來自'UIImage!'的隱含促銷。在下面的代碼爲 '的UIImage!?'」:

if let ImageFinal = self.CharImage as? UIImage! { 

    } 
0

這不是我如何使用塞格斯的sender參數。由於UIViewController正在進行performSegue調用,因此通常我會調用self。像下面的東西應該工作。

import UIKit 

class MainWelcomVC: UIViewController { 

    var ImageArr = [UIImageView]() 
    var CharImage: UIImage? 

    @IBOutlet weak var BackGround: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     for x in 0...2 { 

      let image = UIImage(named: "char\(x).png") 
      let imageview = (UIImageView(image: image)) 
      ImageArr.append(imageview) 
     } 
    } 

    @IBAction func PlayButtonPressed(_ sender: Any) { 

     let randomIndex = Int(arc4random_uniform(UInt32(ImageArr.count))) 
     self.CharImage = ImageArr[randomIndex].image 

     performSegue(withIdentifier: "CharDisplayVC", sender: self) 
    } 



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     if let destination = segue.destination as? CharDisplayVC { 

      if let ImageFinal = self.CharImage as? UIImage { 
       destination.ImageToDisplay = ImageFinal 
      } 
     } 
    } 
} 
+0

仍然給我不能在 CharImage = ImageArr [randomIndex] 和 如果讓ImageFinal = CharImage分配的UIImage到的UIImage視圖作爲問題? UIImage –

+0

我將'self.CharImage = ImageArr [randomIndex]'更新爲'self.CharImage = ImageArr [randomIndex] .image'。我錯過了這個道歉。它現在應該工作。 –

+0

是thanx這個工作太多了,但我已經通過更改數組和其他行的幾個它的工作。你能否在下面的答案中檢查代碼,並告訴我這是否是一種好方法?我也無法確定UI圖像視圖和UIimage之間的差異 –