Reputation: 1035
The problem: Created a view controller in a xib with a table view. But, the table view acts very weirdly and doesn't look like the table view we use in the storyboard.
My plan was to populate this table view with a custom table view cell which I have created in another xib file. But, sadly, it doesn't work as I have expected because everything was off and those cells are instantiated, I know my custom cells work because it has worked in my other view controller that was created in the storyboard:
I wanted a way to design my view controllers so I can just instantiate them when I need, my reasoning behind this is I don't want to have a very populated storyboard. Now I know that I can't use a table view in a xib file like how we use it in a storyboard. Is there a work around to this? do I need another storyboard to achieve this?
Upvotes: 1
Views: 1716
Reputation: 42598
Yes, you can use as many storyboards as you would like in an application.
I try to setup 1 storyboard per workflow. However some less storyboard enthusiastic developers use 1 storyboard per view controller.
To get the initial view controller from a storyboard named "MyStoryboard":
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let viewController = storyboard.instantiateInitialViewController()!
or to get a view controller with the identifier "MyViewController".
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MyViewController")!
There are two basic ways for a view controller to access a second view controller in another storyboard.
In the storyboard, use a Storyboard Reference to the second view controller, then use a show segue to push the second view controller.
In the view controller's source, create the second view controller using instantiateInitialViewController()
or instantiateViewController(withIdentifier:)
and push it onto the navigation controller.
Upvotes: 2
Reputation: 131
Task 1. Load UIViewcontroller form Xib
We can load a UIViewcontroller form Xib instead of Storyboard. We can use following procedure:
1. Create a UIViewcontroller.
XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your class name , subclass of with UIViewController , check Also create Xib file, language Swift -> Next - Create.
Example: ViewControllerFromXib
2. Override init().
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
{
super.init(nibName: "ViewControllerFromXib", bundle: Bundle.main)
}
required init?(coder aDecoder: NSCoder)
{
super.init(coder:aDecoder)
}
3. Open newly created Controller
let controller = ViewControllerFromXib.init()
self.present(controller, animated: true, completion: nil)
In this above way, We can load a UIViewcontroller from XIB.
Task 2. Create a tableview & populate it's cell using custom xib
1. Create a Custom UItableViewCell
XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your cell name , subclass of with TableViewCell , check Also create Xib file, language Swift -> Next - Create.
Example: CustomTableViewCell
1.Register UItableViewCell for Your TableView.
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Item"
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle:Bundle.main), forCellReuseIdentifier: "CustomTableViewCell");
}
2. Implement UITableViewDataSource into Your Viewcontroller
extension ViewControllerFromXib:UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
return cell
}
}
2. Implement UITableViewDelegate into Your Viewcontroller.
extension ViewControllerFromXib:UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension;
}
}
Upvotes: 1
Reputation: 13459
You can have many storyboards in the same project.
I usually like to have 1 storyboard per "main" screen, which is easy to instantiate programmatically, and can be linked in the IB as well.
As for your problem, i'd suggest your custom uitableviewcell be created in a .xib file. As an independent view. This way on your code you can just register it as the "reusable cell" for any view controller you want regardless of the storyboard that contains it.
Upvotes: 0