phbelov
phbelov

Reputation: 2619

Working with pre-setup views in Xcode/Swift

Suppose, I've setup a view somehow in the Storyboard in Xcode. I've also written a UIView subclass – "CustomView" for that view to use with. This CustomView has properties which correspond to the elements which the UIView in the storyboard consists of.

Firstly, can I somehow connect the properties of the CustomView subclass to the UI elements of the view in a Storyboard (a view has labels, images, etc.)?

I know that I can create IBOutlets. But I think that it would not be that efficient to connect all the view's elements to one VC because I have the same CustomViews stacked in UIStackView (overall, there are three CustomViews in UIStackView).

If not, what is the most efficient way to set up such elements programmatically (I want to set the values of the properties via API, so each CustomView would look the same, but its elements would have different values). I don't want to create a view that I've set up in the storyboard completely via code, because adding constraints and laying out subviews would be a huge pain (even though, I know it's possible to do it).

Upvotes: 0

Views: 257

Answers (1)

Abbey Jackson
Abbey Jackson

Reputation: 885

Okay I think what you are asking is that you have a uiview with a stackview inside it, MyCustomUIView. You want to reuse this view three times on the same view controller, MyViewController and load different data into it each time. Is that correct?

If that is the set up that you want then you set all the outlets to MyCustomUIView. On MyViewController, you create three outlets for your three views (view1, view2, view3) and you would programatically load MyCustomView into each view (view1, view2, view3) and then access the labels and other outlets from those views as view1.label, view2.label, etc. This would be easier/cleaner to do if you were using xibs instead of storyboard btw. I personally would do that but if your project is storyboard based you can keep MyViewController on the storyboard and create a xib for MyCustomUIView, then load it as I described above.

Another way you could set this up in storyboard is by using a UITableView on MyViewController with 3 cells instead of your three UIViews. Generally this would be the preferred way for reused views for many reasons. One reason would be if you ever want 4 views, or only 2, it's very simple to change. You don't have to go change all the UI/constraints on MyViewController.

To address your concern regarding programmatic constraints...yep they are a pain. However Apple has added anchors that can be used to simplify programmatic layout constraints: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UILayoutGuide_Class_Reference/index.html#//apple_ref/doc/uid/TP40015257-CH1-DontLinkElementID_13

Or you could use the PureLayout cocoapod which simplifies things further: https://github.com/PureLayout/PureLayout

ADDITONAL INFO

If using a xib and adding it programmatically what you would do is the following:

Create a Cocoatouch class file enter image description here

On the next screen make it a UIViewController and check off "Also create a XIB file". enter image description here

Then in MyViewController:

view1 = NSBundle.mainBundle().loadNibNamed(String(MyCustomView), owner: nil, options: nil).first as! MyCustomView
view1.someLabel.text = "set the text"

view2 = NSBundle.mainBundle().loadNibNamed(String(MyCustomView), owner: nil, options: nil).first as! MyCustomView
view2.someLabel.text = "set the text"

view3 = NSBundle.mainBundle().loadNibNamed(String(MyCustomView), owner: nil, options: nil).first as! MyCustomView
view3.someLabel.text = "set the text"

Other than using a tableview or a collectionview as you mentioned in your comment I actually do not know how to have a reusable view (or if it's possible) in storyboard only without xibs. Perhaps if it's possible someone else will reply :)

Now that you have this basic info, do some googling about xibs if you are still confused. Search stack overflow for "add xib swift" also and you will find many results. And of course reply if you have more questions!

Upvotes: 1

Related Questions