Reputation: 844
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
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
Reputation: 1402
Yo have your ViewControler created with some Objects in it (UILabel, UIButton, UIImage...)
1 - You need to link your ViewControler in your story board to your ViewController.swift, to do this follow the pictures
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
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
Second select the parragraf icon in the right up corner ands clicked 'Assistant', it will open a screen of your ViewControler.swift
Now yopu just need to drag the variable to the corresponding object and you will be done.
REMEMBER TO TO THIS WIHT ALL VARIABLES, YOU WILL NEED TO CREATE A VARIABLE FOR EACH OBJECT YOU HAVE IN THE STORYBOARD.
Upvotes: 1
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
}
Upvotes: 3
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