Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Uniquely identify ViewControllers from Storyboard

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 ViewControllers 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

like image 385
mgold Avatar asked Dec 28 '11 19:12

mgold


2 Answers

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.

like image 87
Tom van der Woerdt Avatar answered Nov 04 '22 10:11

Tom van der Woerdt


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.

like image 45
Mundi Avatar answered Nov 04 '22 10:11

Mundi