One xib, many ViewControllers - is it a proper way?

490 views Asked by At

I'm new to iOS. I'm currently developing an iOS application and I have a small question to you. I have few ViewControllers which Views are the same - a tableView with some data. Only the logic is different - if you select a row, different things happen, also the rows look a little bit different, but those differences are made in the ViewControllers code. My question is, what is the proper way for creating xibs for those ViewControllers? Should I create new xib for each ViewController or just one for all of them? If one, what with the FilesOwner of xib? I set it in xCode, so should I change it or what? Is it good to have only one View? Thank you!

3

There are 3 answers

0
anka On BEST ANSWER

If all views are UITableViews you would only need one xib file (lets name it GenericTableView.xib), add your UITableView, connect it with the file owner which is simply a UITableViewController. After that create separate subclasses of UITableViewController and implement your specific behavior in there.

In your code you can the easily initiate any of your UITableViewController subclasses with

[[MyViewController alloc] initWithNibName:@"GenericTableView" bundle:[NSBundle mainBundle]];
2
CTime95 On

The previous answer is right, but I don't feel confortable instantiating ViewControllers or using xib in code. I let the segues do all the work.

I would have a base class, let's say GenericTableViewController and define common behavior there (like IBActions or interactions with the UI), the view is also inherited so all the subclasses (viewcontrollers) share it.

0
Argus On

Swift 4.2

MyViewController.init(nibName: "MyGenericVC", bundle: Bundle.main)