如何使用类来控制Objective-C中的窗口

时间:2022-06-01 18:59:59

I have been making applications for Mac with Objective-C for about a year now but due to not really understanding how to use classes properly I have only ever used the 'AppDelegate' files. I want to start using classes as soon as possible because from what I understand it's very bad practice to clump it in to one class. Basically, how do I have two windows, each controlled by it's own class. I understand how to make objects similar to NSString or something but don't understand how to have classes that control windows etc.

我用Objective-C为Mac做了大约一年的应用程序,但由于没有真正理解如何正确使用类,我只使用过'AppDelegate'文件。我想尽快开始使用类,因为根据我的理解,将它集中到一个类是非常糟糕的做法。基本上,我如何拥有两个窗口,每个窗口由它自己的类控制。我知道如何使对象类似于NSString或其他东西,但不明白如何控制窗口的类等。

Thanks

谢谢

Edit: Basically I want to know how to split up my application in to classes.

编辑:基本上我想知道如何将我的应用程序拆分为类。

2 个解决方案

#1


1  

If I understand you correctly then you need to create individual controller classes sporting their own IBOutlets and IBActions and hook these up to your UI elements. To split up an existing application into smaller classes requires some knowledge of Object Oriented programming.

如果我理解正确,那么你需要创建单独的控制器类,运行他们自己的IBOutlets和IBActions并将它们连接到你的UI元素。要将现有应用程序拆分为较小的类,需要具备面向对象编程的一些知识。

Alternatively, you might benefit from reading this (or a similar) book:

或者,您可能会从阅读本书(或类似的)书中受益:

'Cocoa Programming for Mac OS X' by Aaron Hillegass.

Aaron Hillegass的“Mac OS X的可可编程”。

#2


1  

Try looking for NSWindowController in the docs. You create a custom subclass of NSWindowController and a xib file for it. In the xib file, make sure you set the class on the File's Owner to your custom subclass, and make sure its window outlet is connected to the window in the xib. If all that sounds totally foreign, head for the books! =)

尝试在文档中查找NSWindowController。您可以为它创建NSWindowController的自定义子类和xib文件。在xib文件中,确保将File's Owner上的类设置为自定义子类,并确保其窗口插座连接到xib中的窗口。如果听起来完全是外国人的话,那就去看书! =)

Then, in the code where you want to bring this window onto the screen, you create an instance of your custom subclass and associate it with the xib, like so:

然后,在要将此窗口带到屏幕上的代码中,创建自定义子类的实例并将其与xib关联,如下所示:

MyCustomWindowController *controller = [[MyCustomWindowController alloc] initWithWindowNibName:@"myxib"]
[controller showWindow:self];

The xib loading system will hook up all your custom outlets and actions on the new controller, and you can show it or do other wonderful NSWindowController things.

xib加载系统将连接新控制器上的所有自定义插座和操作,您可以显示它或做其他很棒的NSWindowController事情。

#1


1  

If I understand you correctly then you need to create individual controller classes sporting their own IBOutlets and IBActions and hook these up to your UI elements. To split up an existing application into smaller classes requires some knowledge of Object Oriented programming.

如果我理解正确,那么你需要创建单独的控制器类,运行他们自己的IBOutlets和IBActions并将它们连接到你的UI元素。要将现有应用程序拆分为较小的类,需要具备面向对象编程的一些知识。

Alternatively, you might benefit from reading this (or a similar) book:

或者,您可能会从阅读本书(或类似的)书中受益:

'Cocoa Programming for Mac OS X' by Aaron Hillegass.

Aaron Hillegass的“Mac OS X的可可编程”。

#2


1  

Try looking for NSWindowController in the docs. You create a custom subclass of NSWindowController and a xib file for it. In the xib file, make sure you set the class on the File's Owner to your custom subclass, and make sure its window outlet is connected to the window in the xib. If all that sounds totally foreign, head for the books! =)

尝试在文档中查找NSWindowController。您可以为它创建NSWindowController的自定义子类和xib文件。在xib文件中,确保将File's Owner上的类设置为自定义子类,并确保其窗口插座连接到xib中的窗口。如果听起来完全是外国人的话,那就去看书! =)

Then, in the code where you want to bring this window onto the screen, you create an instance of your custom subclass and associate it with the xib, like so:

然后,在要将此窗口带到屏幕上的代码中,创建自定义子类的实例并将其与xib关联,如下所示:

MyCustomWindowController *controller = [[MyCustomWindowController alloc] initWithWindowNibName:@"myxib"]
[controller showWindow:self];

The xib loading system will hook up all your custom outlets and actions on the new controller, and you can show it or do other wonderful NSWindowController things.

xib加载系统将连接新控制器上的所有自定义插座和操作,您可以显示它或做其他很棒的NSWindowController事情。