在两个笔尖之间传递参数:建议?

发布于 2024-11-08 19:36:05 字数 468 浏览 5 评论 0原文

在我的主笔尖 (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 技术交流群。

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

发布评论

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

评论(1

記柔刀 2024-11-15 19:36:05

对于这个例子,我假设您有类定义 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 看起来像:

@protocol Nib2Delegate;

@interface Nib2 : UIViewController {
    id <Nib2Delegate> delegate;
}

@property (nonatomic, assign) id <Nib2Delegate> delegate;

@end

@protocol Nib2Delegate 

- (void)myMethod;

@end

在 Nib1.h 中,您将具有:

#import "Nib2.h"

@interface Nib1 : UITableViewController <Nib1Delegate> {

}

- (void)myMethod;

@end

在 Nib1.m 中

Nib2 *nib2 = [[Nib2 alloc] initWithNibName:@"Nib2" bundle:nil];
nib2.delegate = self;

然后,您将在 Nib1.m 中实现 myMethod。当你想在 Nib2.m 中调用它时,它看起来像

[delegate myMethod];

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:

@protocol Nib2Delegate;

@interface Nib2 : UIViewController {
    id <Nib2Delegate> delegate;
}

@property (nonatomic, assign) id <Nib2Delegate> delegate;

@end

@protocol Nib2Delegate 

- (void)myMethod;

@end

in Nib1.h you would have:

#import "Nib2.h"

@interface Nib1 : UITableViewController <Nib1Delegate> {

}

- (void)myMethod;

@end

in Nib1.m

Nib2 *nib2 = [[Nib2 alloc] initWithNibName:@"Nib2" bundle:nil];
nib2.delegate = self;

Then you would implement myMethod in Nib1.m. When you want to call it in Nib2.m it would look like

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