简体   繁体   English

如何在容器视图(嵌入式视图控制器)中更改视图?

[英]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.

相关问题 嵌入式页面控制器到达索引时更改容器视图大小 - change container view size when embedded page controller reaches index 如何在 Swift 中将数据从父视图控制器传递到嵌入式视图控制器? - How can I pass data from a parent view controller to an embedded view controller in Swift? 将数据传递到嵌入在Container View Controller中的View Controller - Pass data to View Controller embedded inside a Container View Controller 如何从嵌入在父视图控制器的子视图控制器中的 UISearchViewController 呈现视图控制器? - How do I present a view controller from UISearchViewController embedded in a child view controller of a parent view controller? 当容器视图控制器中发生某些事情时,如何更新视图控制器? - How can I update a view controller when something happens in a container view controller? 如何在容器视图中从嵌入式控制器获取数据 - How to get data from the embedded controller in a container view 通过在嵌入式容器内的集合视图中选择一个单元格来更改视图控制器中的值/图像 - Change value / image in view controller by selecting a cell within a collection view within an embedded container 如何在容器视图中为父视图控制器中的约束更改设置动画? (快速4) - How do I animate a constraint change in a parent view controller from within a container view? (Swift 4) 如何将数据从View Controller传递到Container View? - How would I pass data from a View Controller to a Container View? 如何快速从父视图控制器访问容器视图中的数据? - How can I access data from a container view from the parent view controller in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM