One xib, multiple classes without inheritance in iOS? - 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

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.

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, several sub classes

I have one xib file for a custom UIView subclass. Works fine. I'm able to load the correct nib and create an instance of my class and it contains all the subviews I added to the xib file.
However, I have also subclassed this view, but I can't figure out how to create an instance of this class and get it to use the xib file used by the parent class. Is this even possible? I don't want to create a new xib file for my subclass, since the view hierarchy, subviews and GUI looks the same, it's just the code that differs.
Can I load a nib and "connect" it to another class than the one specified as the "Custom class" in the xib settings? Or can I create a new instance of a view and tell it to use a xib of a certain name?
You can try to write something really weird with -awakeAfterUsingCoder: to substitute created object, but this is really shaky and a few can get it right.
The thing is that .xib file stores set of serialised objects, when this set is loaded, information about each object, i.e. it's class, size, other attributes, parent object, constraints are as well deserialised and applied. So, xib files store which class should receive +alloc and other messages and, consequently, which objects will then receive all the attributes via KVC (-setValue:forKey:). So, no, you can't just configure some class to load some xib, because xib file tells which class should be loaded.
As a soulution I'd suggest to refactor your code, (for example) incapsulate different subclasses logic to some other object. So, before you had multiple subclasses with different logic, then, you'll have single class, loadable from xib, but you have to set some MyDifferentLogicVariant1Implamentor entity to preserve different logic for 'different' classes.
Superclass - Subclass1 - Subclass2
vs
Superclass.differentLogic = DifferentLogicImplementor1
Superclass.differentLogic = DifferentLogicImplementor2

Can I subclass a custom view controller

I made a subclass of UITableViewController named CalendarTableViewController.h and .m. It shows a list of events within specified range (e.g. all events in 2014). I'd like to have two more view controllers (for month and day) and stack them on top of the first view controller.
Because all three view controllers have similar properties and behaviors, I'd like to subclass my custom view controller. Is this possible?
I'm about a year into developing with Xcode, so I may be missing something stupidly simple. But, all I know is how to subclass UITableViewController. How do you subclass your own custom view controller?
I would appreciate if you could share your wisdom.
Yoshi
In the world of object-oriented programming, objects are categorized into hierarchical groups. Rather than using distinct terms for the different hierarchical levels such as genus or species, objects are simply organized into classes. In the same way that humans inherit certain characteristics as members of family, a class can be set to inherit functionality from a parent class.
When one class inherits from another, the child inherits all the behavior and properties defined by the parent. It also has the opportunity either to define its own additional behavior and properties, or override the behavior of the parent.
Long story short, yes, you can and you should create subclass(es) in such cases. This is how you do in Objective-c in .h file (in your new class)
#interface MySecondController : CalendarTableViewController {
}
In Swift
class MySecondController: CalendarTableViewController {
// subclass definition goes here
}
I hope it helps

Superclass with XIB and its subclass with XIB?

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.

Resources