在两个笔尖之间传递参数:建议?
在我的主笔尖 (nib1) 中,有一个按钮可以使用下面的代码激活另一个笔尖 (nib2)。
我目前将值传递给 nib2 并从 nib2 获取的方法是在 nib1 中创建类方法。但我这个练习项目的目标之一是我需要设计 nib2,以便我能够在以后的另一个项目中重复使用 nib2。这意味着 nib2 不应该知道 nib1 类的任何信息。这样当我以后重新使用 nib2 时,我不需要修改它的代码(调用 nib1 的类方法)。
我正在考虑在 nib2 的类中创建类方法。但那么我如何才能在 nib2 激活之前调用该方法呢?
我的做法对吗?有什么意见吗?
NSWindowController *iQWController = [[NSWindowController alloc] initWithWindowNibName:@"iQueryWindow"];
[iQWController showWindow:sender];
In my main nib (nib1), I have a button that activates another nib (nib2) using the piece of code below.
The way that I currently pass values to nib2 and obtain from nib2 is by creating class methods in nib1. But one of my goal of this exercise project is that I need to design nib2 such that I will be able to re-use nib2 at another later project. That implies nib2 should not know anything about nib1's class. So that when I re-use nib2 at a later time, I do not need to modify its code (to call nib1'class methods.)
I was thinking about creating class methods in nib2's class. But then how will I be able to call the method before nib2 is active?
Is my approach right? any comments?
NSWindowController *iQWController = [[NSWindowController alloc] initWithWindowNibName:@"iQueryWindow"];
[iQWController showWindow:sender];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这个例子,我假设您有类定义 nib1.h/nib1.m/nib1.xib & nib2.h/nib2.m/nib2.xib
您应该在 nib2 中创建属性以将数据获取到 nib 2。
要将数据从 nib 2 获取回 nib 1,您应该定义一个委托协议,让 nib 1 实现它并设置它作为 nib 2 的代表。
nib2.h 看起来像:
在 Nib1.h 中,您将具有:
在 Nib1.m 中
然后,您将在 Nib1.m 中实现 myMethod。当你想在 Nib2.m 中调用它时,它看起来像
For this example I assume you have class definitions nib1.h/nib1.m/nib1.xib & nib2.h/nib2.m/nib2.xib
You should create properties in nib2 for getting the data into nib 2.
To get data from nib 2 back to nib 1, you should define a delegate protocol, have nib 1 implement it and set it as nib 2's delegate.
nib2.h would look something like:
in Nib1.h you would have:
in Nib1.m
Then you would implement myMethod in Nib1.m. When you want to call it in Nib2.m it would look like