简体   繁体   English

iOS:从Storyboard中唯一标识ViewControllers

[英]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. 我有一个自定义的ViewController ,它可以重复使用,并且任意数量的实例将在Storyboard的NavigationController中链接在一起,所有实例都与委托共享相同的模型。

The ViewController s need to tell the model which instance they are. ViewController需要告诉模型它们是哪个实例。 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). 目前,他们有一个他们从segue获得的int属性,但它似乎不是非常惯用的,并且不适合在屏幕上有多个实例(对于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 结果:self.view.tag

A UIViewController's UIView has a tag property which you can set from anywhere you want. UIViewController的UIView有一个tag属性,您可以在任何地方设置它。 You could also simply identify the type of the controller by using [self class] . 您也可以使用[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. 更新您可以使用类别为UIViewController简单地实现唯一标识符。

I guess the "cleanest" way in terms of design architecture would perhaps be an array of ViewControllers. 我想在设计架构方面“最干净”的方式可能是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. (它可以在app委托中进行管理。)但是,有一些内存注意事项 - 在iPhone上你可能想要创建并根据需要销毁视图控制器。 The array could contain the identifier and perhaps some other model-related information in order to recreated the controllers as needed. 该阵列可以包含identifier以及可能的一些其他与模型相关的信息,以便根据需要重新创建控制器。

Too bad there is no property storyboardIdentifier of UIViewController. 太糟糕了,没有UIViewController的属性storyboardIdentifier They can be instantiated with this id but it would be helpful if the viewcontroller can query its id. 它们可以使用此id进行实例化,但如果viewcontroller可以查询其id,将会很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM