我可以传递“参数”吗?从 Interface Builder 中,所以我的 ViewController 类知道如何以不同的方式处理?

发布于 2024-11-30 07:06:21 字数 309 浏览 4 评论 0原文

在我的项目中,我有一个视图(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我乃一代侩神 2024-12-07 07:06:21

那么,您的另一个选择是在视图控制器的视图上使用 tag 参数 - 如果您尚未将其用于其他用途。

然后,您的 viewController 将读取其主视图的标签并相应地更改其行为。

myViewControllersView.tag = i;

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.

myViewControllersView.tag = i;
不回头走下去 2024-12-07 07:06:21

我会考虑在视图控制器中创建您自己的自定义初始化方法,并使用这些方法来初始化您的视图控制器。

myViewController = [[[myUIViewController alloc] init] initWithParamOne:paramOne andParamTwo:paramTwo];

- (id) initWithParamOne:(NSString *)paramOne andParamTwo:(NSString *)paramTwo {
    self = [super initWithNibName:@"myNib" bundle:nil]; 
    if (self) {
        // code here
    }
}

I'd look into creating your own custom init methods in your view controllers and use these to initialise your view controllers.

myViewController = [[[myUIViewController alloc] init] initWithParamOne:paramOne andParamTwo:paramTwo];

- (id) initWithParamOne:(NSString *)paramOne andParamTwo:(NSString *)paramTwo {
    self = [super initWithNibName:@"myNib" bundle:nil]; 
    if (self) {
        // code here
    }
}
夜声 2024-12-07 07:06:21

我最终使用“标题”来传递 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文