iOS:从 Storyboard 中唯一标识 ViewControllers [英] iOS: Uniquely identify ViewControllers from Storyboard

查看:21
本文介绍了iOS:从 Storyboard 中唯一标识 ViewControllers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可重用的自定义 ViewController,任意数量的实例将在 Storyboard 的 NavigationController 中链接在一起,所有实例都共享相同的模型作为代表.

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 需要告诉模型它们是哪个实例.目前,他们有一个从 segue 获得的 int 属性,但它似乎不太习惯,并且不适合在屏幕上显示多个实例(对于 iPad).我认为必须有一种更清洁的方法来做到这一点,所以有人知道它是什么吗?谢谢.

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.

结果:self.view.tag

RESULT: self.view.tag

推荐答案

UIViewController 的 UIView 有一个 tag 属性,你可以在任何地方设置它.您还可以使用 [self class] 简单地识别控制器的类型.或者简单地通过直接引用控制器来使用内存位置.

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.

更新您可以简单地使用类别为 UIViewController 实现一个唯一标识符.

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

这篇关于iOS:从 Storyboard 中唯一标识 ViewControllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆