简体   繁体   English

如何将图像视图及其属性从一个视图控制器传递给另一个视图控制器?

[英]How do I pass an image view and its properties from one view controller to another?

So I have an image view in one viewcontroller.swift file that generates a random background image every time the a question is asked (it's a quiz app). 因此,我在一个viewcontroller.swift文件中有一个图像视图,该文件每次询问问题时都会生成一个随机的背景图像(这是一个测验应用程序)。 When the user get's the answer right I want the random background image produced from the first view controller when the question is asked to be the same background image for the next view controller that displays when the answer is answered correctly. 当用户得到正确的答案时,我希望当问题被问到时,从第一个视图控制器生成的随机背景图像与在正确回答答案后显示的下一个视图控制器的背景图像相同。 How do I make sure the both background image views displays the same random image every time a question is correctly answered? 如何确保每次正确回答问题后,两个背景图像视图都显示相同的随机图像? Here is a snippet of my code that deals with the background image. 这是我处理背景图片的代码片段。

class ViewController : UIViewController

@IBOutlet weak var backgroundImage: UIImageView!

let backgroundImages = //array containing all the images


//random image generator function
func randomImage() {
    index = Int(arc4random_uniform(UInt32(backgroundImages.count)))
    backgroundImage.image = backgroundImages[index]
}

Now if I wanted an image view to always display the same background images produced by the randomImage function in a different class (class SecondViewController: UIViewController) for example what would be the best way to get the data from the original view controller? 现在,如果我希望图像视图始终在不同的类(类SecondViewController:UIViewController)中显示randomImage函数产生的相同背景图像,例如,从原始视图控制器获取数据的最佳方法是什么? (If you click the right answer button according to the question it proceeds to a new "right answer" view controller that I want to have the same background image as the previous view controller). (如果您根据问题单击正确的答案按钮,它将转到新的“正确的答案”视图控制器,我希望它具有与以前的视图控制器相同的背景图像)。

First you have to store the background image index that you will set in func randomImage() . 首先,您必须存储将在func randomImage()中设置的背景图像索引 So you have to declare a class variable 所以你必须声明一个类变量

class ViewController : UIViewController

@IBOutlet weak var backgroundImage: UIImageView!
let randomIndex:Int!
let backgroundImages = //array containing all the images


//random image generator function 
func randomImage() {
    // So now you can pass this value to next viewController.
    randomIndex = Int(arc4random_uniform(UInt32(backgroundImages.count)))
    backgroundImage.image = backgroundImages[randomIndex]

}

您可以使用情节提要segue并与prepare语句共享图像,否则可以创建第二个vc的实例,然后需要初始化一个图像视图,而不是需要从第一个VC设置该图像视图并推送该vc。

制作UIImageView的子类并使用它。

You can save the image as a png in the user document folder and reload it as needed. 您可以将图像另存为png在用户文档文件夹中,并根据需要重新加载。 See my recent answer to an unrelated question: 请参阅我最近对一个不相关问题的回答:

Saving an image to User Documents folder 将图像保存到“用户文档”文件夹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller? 如何将UI图像从一个视图控制器传递到另一个? - How to pass UI image from one view controller to another? 当数据从一个视图 Controller 传递到另一个视图时,我如何能够传递图像? - How would I be able to pass an image when data is passed from one View Controller to another? 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? 使用Firebase将图像从一个视图控制器传递到另一个(swift) - pass an image from one view controller to another (swift) with Firebase 使用Swift,如何从另一个视图控制器中的按钮调用在一个视图控制器中编码的函数? - Using Swift, how do I call a function coded in one view controller from a button in another view controller? 如何在一个视图 controller 从另一个视图 controller 访问变量? - How do I access a variable in one view controller from another view controller? 无法将值从一个View Controller传递到另一个View Controller - Not able to pass value from one View Controller to another View Controller 如何将自定义属性从Mapbox注释传递到其标注视图? - How do I pass in custom properties from Mapbox annotation to its callout view? 如何将ID从具有地图视图的视图控制器传递到另一个视图控制器? - How do you pass id from a view controller with a map view to another view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM