Superclass with XIB and its subclass with XIB? - ios

In my UI, there are three different table view cells. Well,not THAT different, they have some elements in common. So a base class capturing common elements would be in order.
How to apply basic OOP principles when I have the base class with XIB file?
How would I then add missing differing elements in respective subclasses if they ALSO have XIB files? (I want them to have XIB files as I am too lazy to edit frames and other properties of those elements in code..who has time for that nowadays...)
I mean how do I solve this superXIB - subXIB relationship thing?

Interesting.. But there is no way you can load multiple xibs from the bundle for a view or merge multiple xibs into one! :)
So I think you cannot have a superXIB - subXIB relationship.
You can create a base Table cell with no xib, containing some common things. Other tableview cells derive this. Have separate xib's for these subviews so that you can set frame and stuff.

Related

inheritance for UI in swift project

I have a class with 5 subclasses. these classes have many views in common. but all of them have a view which is just for them.
I created a view controller with xib file and I added the common views to that xib file (general.xib)
and for each subclass, I created a specific view for itself.
I don't know about the best practice.
is it good to have a UIStackView in general.xib for this situation?
UIStackView will arrange all its subclasses equally/ratio according to our specification. If you want them equally align within you super without any scroll, it's a good one.

Is it a good idea to use xib files in storyboard

I have a project which is based on storyboard. The problem is some views are repeated in some view controllers.
I created xib file for these repeated views and then I use don't create theses view again. I just add a uiview and set its class to the custom view I have created.
It work ok as it avoids repeated view changes.
But I don't know if this cause performance or any other problems later.
And I cannot change the whole project to only use with xib files.
one of the clean code goals is to reuse your code. so base on this, xib file is a good solution, but about performance, using interface builder has less performance than implementation through code. if you are concern too much about performance, use the code, if not, your implementation is ok.
If your project has similar kind of design or view then yes using xib with storyboard is good. you can reduce redundancy by taking similar kind of view or Tableview cell or collectionview cell. that will also make your storyboard neat and clean.
you can check this and based on that you can use as per your requirement.
Which is more efficient way? StoryBoard or XIB?

Creating and Using Custom UITableViewCell Template in Storyboard

I have a UITableView where I'd like to have multiple types of UITableViewCells. The different types of cells have a lot of similar properties, but have a few key differences, so I was hoping to create a template UITableViewCell class (TemplateCell) that the different cell types could extend.
I've tried doing this by creating a the TemplateCell class and having an associated .xib file. I then tried to go to my UITableView storyboard file and created my various cell types that subclassed my template. However, when I added those cell types to the storyboard file, they showed up as blank and didn't have any of the properties I had in the template file.
Is there an better way to create UITableViewCell templates?
You can create a TemplateCell class and separate classes for every other Cell (inherited from TemplateCell). Then you could assign common IBOutlets of EVERY subclassed cell to it TemplateCell class. Or you could operate with them by looking for the views with specific tags if you don't want to bother with outlets for base class.
Unfortunately you can't draw views in IB respecting some base view, so you should draw each cell separately, but you can use common outlets declared in the base class, and those outlets (properties) that differ, put into inherited cell classes.
Without 3d-parties IB doesn't support loading views from xib to a storyboard or another xib. You can use XXNibBridge for that.

One xib, multiple classes without inheritance in iOS?

I have two classes and one xib. I can't simply use one parent class which will be linked with this xib because it means multiple classes (and of course it is not acceptable for me to insert an object of one parent class into the object/class of the another one).
So is it possible to link multiple classes with one xib if they have the same IBOutlet variables but fully different classes?
No, Its not possible to assign multiple classes to one single XIB.
Instead create multiple UIView & load it on one single XIB controller

How can I customize instances of a template xib

I'm learning about xib files and just starting to understand why people use them as well as or instead of storyboards. My question is about how and when it's appropriate to use the xib as a "template".
Let's say I have a xib mapped to my custom UIView subclass - I know how to set that up in IB - and my xib has a UILabel subview. This is a very simplified example just for the purposes of the question, but basically I'm trying to create a view that can be reused for each screen of an iOS "introduction" walkthru, like the panels of https://github.com/MatthewYork/MYBlurIntroductionView
So I want to set most of what's in the view up at design time, and most of it will be common to each instance. The text I want to put in the UILabel is going to be static (i.e. I know it now at design-time) but each instance of the view will have different text. So let's say that I want to position the UILabel in different places in each instance, depending on how much text is in it etc and whether it's covering something else important. Now, I know I could do this programmatically, i.e. have the label as a #property linked up in IB and then set frame position in the code, but as far as I understand it the beauty of using xibs is that you can do known things like this at design-time.
As far as I can see my options are:
Load new instances of the xib and set the position etc programmatically as mentioned above (would rather not if possible)
Create my template xib, setting all the common stuff, and then make copies of it "CustomView1.xib", "CustomView2.xib", etc. (a bit yuck but not too bad)
After creating my template xib, use the storyboard to drag in new UIViews and somehow set each to be linked to my one "CustomView.xib", and then somehow do my static repositioning of the subview UILabel within each of those UIView instances on the storyboard. Is that possible? If so that'd be great. Obviously I know I can set each of those UIViews to be instances of my UIView subclass, but I'm just missing the link between doing that and customising each instance. Does the file's owner have something to do with it?

Resources