gpt4 book ai didi

ios - swift/iOS8 : set imageView for next View Controller before Segue

转载 作者:搜寻专家 更新时间:2023-11-01 05:51:26 24 4
gpt4 key购买 nike

根据一些参数,我试图在第二个 VC 上设置一个图像,但不知何故它总是返回 null??

这就是我所做的:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showDetail" {
println("going forth!")
println(self.myButton.selected)

let controller = segue.destinationViewController as? DetailVC

if myButton.selected == true {
var newImage = UIImage(named:"Test1")
var oldImage = controller?.imageView.image
println(oldImage)

oldImage = newImage
} else {
var newImage = UIImage(named:"Test2")
var oldImage = controller?.imageView.image
println(oldImage)

oldImage = newImage
}

} else {
println("roflcopter")
}
}

detail VC基本“一无所有”:

import UIKit

class DetailVC: UIViewController {


@IBOutlet weak var imageView: UIImageView!

var testString:String!

@IBAction func goBacktoMain(segue: UIStoryboardSegue) {

println("going back")

}

override func viewDidLoad() {
super.viewDidLoad()

println("view Did Load")
println(testString)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

}

第二个 VC 通过 Storyboard 设置了默认图像,但是当我使用断点 @let Controller 停止时,它会告诉我 imageview 实际上是空的,这是否只在细节 vc 显示一次后才设置?

€dit:设置字符串并在第二个 VC 上打印出来效果非常好

最佳答案

你在哪里设置图像?在 prepareForSegue 中,UIImageView IBOutlet 将不会加载。您必须像下面这样在 DetailVC 类中声明一个图像变量,并在 viewDidLoad 中设置该图像。

class  DetailVC
{
var image:UIImage? = nil
@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad()
{
imageView.image = image
}
}

在您的prepareSegue 函数中,设置

let controller = segue.destinationViewController as? DetailVC
controller?.image = //Your Image according to conditions.

关于ios - swift/iOS8 : set imageView for next View Controller before Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27919927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com