IOS: Unique Identification of ViewControllers from the Storyboard

I have a custom ViewController that is designed to be reused, and an arbitrary number of instances will be connected together in the NavigationController in the Storyboard, all of which will use the same model as the delegate.

ViewController must specify the model in which they are presented. They currently have an int property that they get from segue, but it does not seem very idiomatic and does not lend itself to multiple instances on the screen (for iPad). I believe there should be a cleaner way to do this, and does anyone know what that is? Thanks.

RESULT: self.view.tag

+5
source share
4 answers

UIViewController UIView has a tag property that you can set from anywhere you want. You can also simply determine the type of controller using [self class] . Or just use the memory location, directly referring to the controller.

Update You can simply implement a unique identifier for the UIViewController using the category.

+8
source

I think the β€œcleanest” way in terms of design architecture would probably be an array of ViewControllers. (This could be managed in the application delegate.) However, there are memory considerations - on the iPhone, which you most likely want to create, and destroy the view controllers as needed. An array may contain identifier and possibly some other model-related information to recreate controllers as needed.

+1
source

Too bad there is no storyboardIdentifier UIViewController property. They can be created using this identifier, but it would be useful if the viewcontroller can request its identifier.

0
source

I recently came across this. I realized that you can add a "Recovery ID" to the storyboard. Then you can access it, for example, like this (depending on your use case)

 navigationController?.viewControllers.first?.restorationIdentifier 
0
source

Source: https://habr.com/ru/post/1388362/


All Articles