Reputation: 6366
I have a custom ViewController
that is meant to be reusable, and an arbitrary number of instances will be chained together in a NavigationController
in Storyboard, all sharing the same model as a delegate.
The ViewController
s need to tell the model which instance they are. Currently, they have an int property that they get from the segue, but it doesn't seem very idiomatic and doesn't lend itself to having multiple instances onscreen (for iPad). I figure there's got to be a cleaner way to do this, so does anyone know what it is? Thanks.
RESULT: self.view.tag
Upvotes: 6
Views: 6396
Reputation: 419
I recently ran into this. I figured out you can add a "Restoration ID" in the storyboard. Then you can access it perhaps like this (depending on your use case)
navigationController?.viewControllers.first?.restorationIdentifier
Upvotes: 0
Reputation: 748
Too bad there is no property storyboardIdentifier
of UIViewController. They can be instantiated with this id but it would be helpful if the viewcontroller can query its id.
Upvotes: 0
Reputation: 29985
A UIViewController's UIView has a tag
property which you can set from anywhere you want. You could also simply identify the type of the controller by using [self class]
. Or simply use the memory location by referencing the controller directly.
Update You could simply implement a unique identifier for a UIViewController using a category.
Upvotes: 9
Reputation: 80273
I guess the "cleanest" way in terms of design architecture would perhaps be an array of ViewControllers. (It could be managed in the app delegate.) However, there are memory considerations - on the iPhone you would likely want to create and the destroy the view controllers as needed. The array could contain the identifier
and perhaps some other model-related information in order to recreated the controllers as needed.
Upvotes: 1