因此,经过研究,研究和外包,我无法解决我的问题:(。我目前正在尝试习惯于在传递价值时观察视图。那么最好开发一个简单的应用程序吗?
我的应用程序是这样的......第一页有一个图像数组,当视图加载时随机吐出一个图像,一个动作但是使用segue preform函数触发视图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 :(得分:0)
这不是我在segues中使用sender
参数的方式。通常我会调用self
,因为UIViewController正在进行performSegue
调用。以下内容应该有效。
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
}
}
}
}
答案 1 :(得分: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! {
}