IBOutlet for element in containerview inside original viewcontroller

IBOutlet for element in containerview inside original viewcontroller

在我的主视图中,我有两个在视图控制器中有一个 IBOutlet 的容器。

我在两个容器中都有一个图像和一个标签,如下图所示。

我想要一个 IBOutlet 来更改图像和标签,但是当我将它拖到原始视图控制器时它不允许。

所以在 viewcontroller.swift 中,正如我所说,我可以通过单击和拖动来访问每个容器。像这样

@IBOutlet weak var containerview1: UIView!
@IBOutlet weak var containerview2: UIView! 

但我正在尝试访问容器中的图像视图和标签,如下所示:

@IBOutlet weak var containerview1: UIView!
@IBOutlet weak var containerview2: UIView!

@IBOutlet weak containerview1_ImageView: UIImageView!
@IBOutlet weak containerview2_ImageView!: UIImageView!

我知道这可能不是正确的方法。我需要能够通过 viewcontroller.swift.

以编程方式更改两个容器视图中的图像和标签

为容器

创建两个单独的class
import UIKit

class ContainerView1: UIView {
 
    @IBOutlet var containerView1Label: UILabel!
    @IBOutlet var containerView1ImageView: UIImageView!
}

class ContainerView2: UIView {

    @IBOutlet var containerView2Label: UILabel!
    @IBOutlet var containerView2ImageView: UIImageView!
}

在主要 viewController 情节提要中定义那些 class

现在通过从情节提要中拖动来设置标签和图像视图插座

现在在主视图控制器中拖动容器出口并使用

import UIKit

class ViewController: UIViewController {

    @IBOutlet var containerView1: ContainerView1!
    @IBOutlet var containerView2: ContainerView2!

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // use like this both container elements
    
    containerView1.containerView1Label.text = "Container view 1 lable"
    //containerView1.containerView1ImageView.image = yourImage file
    }

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

}