我可以传递“参数”吗?从 Interface Builder 中,所以我的 ViewController 类知道如何以不同的方式处理?
在我的项目中,我有一个视图(在 xib 中定义),它是一个可重用组件,并将放置在许多其他视图中,我的视图控制器类旨在处理该视图的不同模式。
我的应用程序有 3 个选项卡,基本上使用相同的视图,通过使用不同的参数进行初始化,它们工作得非常好。我从 Interface Builder 加载此视图(通过指定 NIB 名称和类名称),但我必须传递不同的参数,以便我的视图控制器知道它是什么模式。
我想知道我可以从 Interface Builder 定义一些“参数”,这样我的视图控制器类就可以简单地查找并确定自身。所以我不需要编写任何额外的代码来在许多不同的地方重用这个笔尖?
In my project I have a view (defined in a xib) that is a reusable component and will be places in many other views, I have my view controller class designed to handle different modes for this view.
My apps have 3 tabs basically use this same view, by initialize with different parameters they work pretty nice. I load this view from the Interface Builder (by specify the NIB name and class name), but I will have to pass different parameters so my view controller will know which mode it is.
I am wondering can I define some 'parameter' from Interface Builder, so my view controller class can simply look up and determinate itself. So I don't need to write any additional code to reuse this nib in many different places?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么,您的另一个选择是在视图控制器的视图上使用 tag 参数 - 如果您尚未将其用于其他用途。
然后,您的 viewController 将读取其主视图的标签并相应地更改其行为。
Your other option then would be to use the tag parameter on the view controller's view - if you're not already using it for something else.
Your viewController would then read it's main view's tag and change it's behaviour accordingly.
我会考虑在视图控制器中创建您自己的自定义初始化方法,并使用这些方法来初始化您的视图控制器。
I'd look into creating your own custom init methods in your view controllers and use these to initialise your view controllers.
我最终使用“标题”来传递 json 字符串,它对我来说效果很好,我不确定是否有更好的方法。
I end up use the "title" to pass a json string and it works for me pretty well, I am not sure if there is a better way to do so.