jabe
jabe

Reputation: 844

In iOS, how to connect a Label to a View using a storyboard?

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.

Upvotes: 2

Views: 6711

Answers (4)

Doug Null
Doug Null

Reputation: 8327

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

Upvotes: 0

Iker Solozabal
Iker Solozabal

Reputation: 1402

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.

Upvotes: 1

Russell
Russell

Reputation: 5554

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

Upvotes: 3

Marek Staňa
Marek Staňa

Reputation: 564

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

Upvotes: 0

Related Questions