7

In my main view I have two containers that have an IBOutlet in view controller.

In both the containers I have a an image and a label as in the picture below.

enter image description here

I want to have an IBOutlet to change the image and label but when I drag it to the original view controller it doesn't allow it.

So in the viewcontroller.swift as I said I can access each container by clicking and dragging. Like this

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

But I am trying to access the image view and labels in the container, something like this:

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

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

I understand that this is probably not the correct way to do it. I need to be able to change the image and label in both of the container views programmatically through viewcontroller.swift.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89
  • Do you want your both container views (image, label) in one viewController ? Given image showing other two viewcontroller with segue. – Sofeda Aug 14 '16 at 05:48
  • I am not sue I understand you question. That is my actual storyboard. I need the viewcontroller.swift to be able to update the pictures and the labels in both the other containerviews. – Nicholas Muir Aug 14 '16 at 05:52
  • i am seeing here three viewControllers. – Sofeda Aug 14 '16 at 05:55
  • The other two view controller are part of the conatinerview and are automatically created when you drop the containerview in. If you want to think of it that way, I am trying to set the elements of the second two view controllers from the first, – Nicholas Muir Aug 14 '16 at 05:56
  • Oh. i create a solution. not sure now to give it as answer. You just need outlets in main view controller? Will i post the solution as answer? – Sofeda Aug 14 '16 at 06:00
  • container views are just UIView or something else? – Sofeda Aug 14 '16 at 06:00
  • They are just UIView – Nicholas Muir Aug 14 '16 at 06:01
  • Possible duplicate of [IBOutlet link to embedded view controller](https://stackoverflow.com/questions/12676660/iboutlet-link-to-embedded-view-controller) – pkamb Sep 21 '21 at 01:35

1 Answers1

2

Create two separate class for containers

import UIKit

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

class ContainerView2: UIView {

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

In main viewController storyboard define those class

enter image description here

Now set the label and imageview outlet by dragging from storyboard

enter image description here

Now drag container outlets in main view controller and use

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.
    }

}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Sofeda
  • 1,361
  • 1
  • 13
  • 23
  • Awesome thank you so much @SMi !. I was completely about to do it incorrectly, that is so much easier than I thought. Thanks again – Nicholas Muir Aug 14 '16 at 06:40
  • 1
    I am getting the error "unexpectedly found nil while unwrapping the optional value" on this line: containerView1.containerView1Label.text = "Container view 1 lable". The only part of your explanation I didn't understand was when you wrote: "In main viewController storyboard define those class". I think that may be the problem. – Nicholas Muir Aug 14 '16 at 07:15
  • "In main viewController storyboard define those class" by this i mean those UIView recognise ContainView1 class through storyboard when you dragging outlet of the container to ViewController class.I have created a demo project for this. and in my side it is working without error. If you give your mail address i can mail it. – Sofeda Aug 14 '16 at 07:39