[英]How can I change the view in a container view (embedded view controller)?
So I have been trying to do this for aa while, that I need to to do is be able to change the image view that a container view embeds. 所以我已经尝试了一段时间了,我需要做的是能够更改容器视图嵌入的图像视图。 Is this possible?
这可能吗?
I wasn't sure if it was, so I thought that maybe I could just make an embedded view for all of the different views and have them all on top of each other - then just bring the one that the user has chosen to the view to the top. 我不确定是否是这样,所以我想也许我可以为所有不同的视图制作一个嵌入式视图,并将它们放在彼此的顶部-然后将用户选择的视图带到视图中到顶部。
I'm pretty sure the second way would work, but it would probably slow down the app and isn't very tidy. 我很确定第二种方法会起作用,但是它可能会使应用程序变慢并且不太整洁。
I would prefer to use the first way and just be able to choose which view controller the view controller embeds. 我宁愿使用第一种方法,也只能选择视图控制器嵌入哪个视图控制器。
I know that stack overflow likes to see that the OP has made an attempt at solving the problem, but I am asking more here if it is possible to do the first way - if it is, how would I go about doing it. 我知道堆栈溢出喜欢看到OP尝试解决问题,但是我在这里问更多是否可以做第一种方法-如果可以,我将如何去做。 If it isn't would my second option be feasible?
如果不是,我的第二种选择是否可行?
Thanks 谢谢
One way is as shown below. 一种方法如下所示。
import UIKit
class ViewController: UIViewController {
var imgNumber = 0
var images = ["image0","image1","image2"]
@IBAction func btn(sender: UIButton) {
imgNumber += 1
if (imgNumber > 2) {
imgNumber = 0
}
imageToBeDisplayed.image = UIImage(named: images[imgNumber])
}
@IBOutlet weak var imageToBeDisplayed: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageToBeDisplayed.image = UIImage(named: images[0])
// 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.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.