2

I'm making a simple iOS app and I've seen that you can connect a Label to a view controller using the storyboard by holding the control button and dragging from the Label to the view controller, creating an IBOutlet. I like how convenient this is.

I'm wondering how to make an IBOutlet to a view, not the view controller. I've made a custom view and put some Labels on it. I want these labels to be referenced in my view class, not the view controller. But XCode doesn't seem to let me make an IBOutlet unless I drag to a view controller. Is there a way around this?

The only other option I can think of is to create the Labels on the view programmatically, but I would rather not do this. I'm trying to keep the labels as part of the view, I don't think the view controller needs to know about them.

On the possible duplicate: My question is specific about trying to connect a Lable (or any control really) to a view rather than a view controller using the storyboard. The question linked is not specific, and seems more like a new user asking for help using xcode. The accepted answer there is not helpful to my question.

jabe
  • 784
  • 2
  • 15
  • 33
  • You'll learn a lot about what to do from this answer: http://stackoverflow.com/questions/9282365/load-view-from-an-external-xib-file-in-storyboard - pretty much you make a Xib and a custom UIView class and use the class as the xib file's owner, then use the assistant editor with the xib selected and you can drag properties like storyboard. – Fred Faust Mar 03 '17 at 14:35
  • There's a lot of tutorials on this. Just search Google. – JoakimE Mar 03 '17 at 14:42

4 Answers4

3

I feels like a workaround, but if you add the outlet to your custom view class first, then you can attach the outlet by dragging from the Referencing Outlet in XCode back to the View in Storyboard

class CustomView: UIView
{
    @IBOutlet weak var lblOne: UILabel!

    // do all the other stuff you need
}

enter image description here

Russell
  • 5,436
  • 2
  • 19
  • 27
  • This works great. It does feel like a workaround though, do you think this is hidden on purpose? Or do you think it's just a quirk of XCode? – jabe Mar 03 '17 at 15:09
1

Yo have your ViewControler created with some Objects in it (UILabel, UIButton, UIImage...)

enter image description here

1 - You need to link your ViewControler in your story board to your ViewController.swift, to do this follow the pictures

enter image description here

2 - In the class filed put the name of the ViewController class. with that you just linked your storyBoard view controller to your viewController.swift class

enter image description here

3 - Now you need to cretae the variables you want to asign(UILabel, UIButton ... that you have in your storyboard): in this example:

`class MovieDetailViewController: UIViewController, DetailView {

var presenter: MovieDetailPresenter!
var movie: Movie?

@IBOutlet weak var lblMovieYear: UILabel!
@IBOutlet weak var lblMovieTitle: UILabel!
@IBOutlet weak var movieImage: UIImageView!
@IBOutlet weak var lblMovieRating: UILabel!
@IBOutlet weak var lblMovieOverview: UILabel! 

}`

4 - To link the UILabel in the story board to your UILabel variable or your UIButton in your story board to your UIButton var, follow the next images

First select the view controller in the storyboard

enter image description here

Second select the parragraf icon in the right up corner ands clicked 'Assistant', it will open a screen of your ViewControler.swift

enter image description here enter image description here

Now yopu just need to drag the variable to the corresponding object and you will be done.enter image description here

REMEMBER TO TO THIS WIHT ALL VARIABLES, YOU WILL NEED TO CREATE A VARIABLE FOR EACH OBJECT YOU HAVE IN THE STORYBOARD.

Iker Solozabal
  • 1,232
  • 11
  • 16
0

first you have to take parent view in storyboard (view where are you adding labels and declare that this view is view of class that you have in code). Assume, you will create class MyView which inherits from UIView, then you will go to storyboard, click on that parent view in storyboard where are your labels and in right section choose class and write there name of your previously created class (MyView) - autocompletion should work. then open storyboard on one side and your MyView class on right side and you can drag and drop reference exactly the same as with the ViewController

Marek Staňa
  • 522
  • 7
  • 10
  • This is exactly what I tried to do in the first place. Does it work for you? – jabe Mar 03 '17 at 15:50
  • Yes it is working for me. Do you have any error or that's problem? – Marek Staňa Mar 03 '17 at 15:56
  • I set it up like you said. My view has a custom class "MyView". When I have the storyboard on the left and MyView's interface on the right, I am unable to drag labels inside the view to create an IBOutlet. It works fine if I drag from the storyboard to my *view controller*, but that is why I'm confused. – jabe Mar 03 '17 at 16:27
0

Example connection of label to code.

Hold ctrl down and drag from label to within your viewController class. It asks you to name the outlet, eg. heart_rate_UILabel

@IBOutlet weak var heart_rate_UILabel: UILabel!

Use the name to programmatically set the label.
eg.

heart_rate_UILabel.text = String( wearable_heart_rate )

eg, from a non-main thread... DispatchQueue

.main.async
        {
            self.heart_rate_UILabel.text = String( self.wearable_heart_rate)
}
Doug Null
  • 7,989
  • 15
  • 69
  • 148