I have a project for iPhone and iPad. The iPad needs a split view controller. Do we create two separate storyboards? One for iPhone (using autolayout we can support all devices) and two for iPad. My doubt is the difference it only in initial view. The inside views repeats. How will be your approach with storyboard ?
Ios – Do we need to use 2 Storyboard for a Universal app
iosios-universal-appstoryboard
Related Solutions
As I understand it, you want to:
- Design a cell in IB which can be used in multiple storyboard scenes.
- Configure unique storyboard segues from that cell, depending on the scene the cell is in.
Unfortunately, there is currently no way to do this. To understand why your previous attempts didn't work, you need to understand more about how storyboards and prototype table view cells work. (If you don't care about why these other attempts didn't work, feel free to leave now. I've got no magical workarounds for you, other than suggesting that you file a bug.)
A storyboard is, in essence, not much more than a collection of .xib files. When you load up a table view controller that has some prototype cells out of a storyboard, here's what happens:
- Each prototype cell is actually its own embedded mini-nib. So when the table view controller is loading up, it runs through each of the prototype cell's nibs and calls
-[UITableView registerNib:forCellReuseIdentifier:]
. - The table view asks the controller for the cells.
- You probably call
-[UITableView dequeueReusableCellWithIdentifier:]
When you request a cell with a given reuse identifier, it checks whether it has a nib registered. If it does, it instantiates an instance of that cell. This is composed of the following steps:
- Look at the class of the cell, as defined in the cell's nib. Call
[[CellClass alloc] initWithCoder:]
. - The
-initWithCoder:
method goes through and adds subviews and sets properties that were defined in the nib. (IBOutlet
s probably get hooked up here as well, though I haven't tested that; it may happen in-awakeFromNib
)
- Look at the class of the cell, as defined in the cell's nib. Call
You configure your cell however you want.
The important thing to note here is there is a distinction between the class of the cell and the visual appearance of the cell. You could create two separate prototype cells of the same class, but with their subviews laid out completely differently. In fact, if you use the default UITableViewCell
styles, this is exactly what's happening. The "Default" style and the "Subtitle" style, for example, are both represented by the same UITableViewCell
class.
This is important: The class of the cell does not have a one-to-one correlation with a particular view hierarchy. The view hierarchy is determined entirely by what's in the prototype cell that was registered with this particular controller.
Note, as well, that the cell's reuse identifier was not registered in some global cell dispensary. The reuse identifier is only used within the context of a single UITableView
instance.
Given this information, let's look at what happened in your above attempts.
In Controller #1, added a prototype cell, set the class to my UITableViewCell subclass, set the reuse id, added the labels and wired them to the class's outlets. In Controller #2, added an empty prototype cell, set it to the same class and reuse id as before. When it runs, the labels never appear when the cells are shown in Controller #2. Works fine in Controller #1.
This is expected. While both cells had the same class, the view hierarchy that was passed to the cell in Controller #2 was entirely devoid of subviews. So you got an empty cell, which is exactly what you put in the prototype.
Designed each cell type in a different NIB and wired up to the appropriate cell class. In storyboard, added an empty prototype cell and set its class and reuse id to refer to my cell class. In controllers' viewDidLoad methods, registered those NIB files for the reuse id. When shown, cells in both controllers were empty like the prototype.
Again, this is expected. The reuse identifier is not shared between storyboard scenes or nibs, so the fact that all of these distinct cells had the same reuse identifier was meaningless. The cell you get back from the tableview will have an appearance that matches the prototype cell in that scene of the storyboard.
This solution was close, though. As you noted, you could just programmatically call -[UITableView registerNib:forCellReuseIdentifier:]
, passing the UINib
containing the cell, and you'd get back that same cell. (This isn't because the prototype was "overriding" the nib; you simply hadn't registered the nib with the tableview, so it was still looking at the nib embedded in the storyboard.) Unfortunately, there's a flaw with this approach — there's no way to hook up storyboard segues to a cell in a standalone nib.
Kept prototypes in both controllers empty and set class and reuse id to my cell class. Constructed the cells' UI entirely in code. Cells work perfectly in all controllers.
Naturally. Hopefully, this is unsurprising.
So, that's why it didn't work. You can design your cells in standalone nibs and use them in multiple storyboard scenes; you just can't currently hook up storyboard segues to those cells. Hopefully, though, you've learned something in the process of reading this.
After some digging through the storyboard source code, it turns out that the iPad storyboard was copied from the iPhone storyboard. So, the question really became how do I convert an iPhone storyboard into an iPad storyboard?
The answer is surprisingly simple. I ran across this SO answer -- to convert an iPhone storyboard to an iPad storyboard, do the following:
From Xcode, right-click on the storyboard and choose Open As -> Source code
Search for
targetRuntime="iOS.CocoaTouch"
and change it totargetRuntime="iOS.CocoaTouch.iPad"
Right-click on the storyboard again and choose Open As -> iOS Storyboard
The storyboard will now show all views in the correct size.
Related Topic
- Objective-c – How to use single storyboard uiviewcontroller for multiple subclass
- Ios – Best practices for Storyboard login screen, handling clearing of data upon logout
- Ios – How/whether to make a universal storyboard in Xcode
- Ios – How to migrate from iPhone/iPad storyboards to universal storyboard
- Swift – Universal application with Portrait orientation for iPhone and Landscape orientation for iPad
Best Answer
No need for two storyboards any more in iOS 8! We can use Adaptive UI in order to tailor the same storyboard all different sizes of iPad and iPhone.
You can find a nice tutorial in here http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial
And also you can find a nice video by apple in WWDC conference here and search for "Building Adaptive Apps with UIKit"
Basically we can handle different screen sizes by defining elements of our UI in storyboard for different size classes. We can also define different Auto layout constraints for different size classes.
And these are all possible size classes in iOS 8 ( I took the image from https://medium.com/@getaaron/ios-8-development-tips-for-iphone-6-and-iwatch-1c772554ffe0)