gpt4 book ai didi

objective-c - cocoa 教程麻烦

转载 作者:行者123 更新时间:2023-12-03 17:14:33 26 4
gpt4 key购买 nike

它是 this 的重复项问题。

我在掌握一些 Cocoa 基础知识(特别是 Controller 和委托(delegate))时遇到困难,所以我决定完成一个基本教程。我找到了“您的第一个 Mac 应用程序”,一切都很顺利,直到“Where To Next”阶段。以下是一些我不明白的大概基本的事情:

>

  • "Create a new controller class, and move for managing the track and the user interface from the application delegate to this new class."

...创建单独的 Controller 类有什么意义?我得到了 MVC 模式并在过去使用过它,我只是不知道在那里单独的 Controller 和委托(delegate)的位置。

  • "Create an instance of the controller class in the nib file, and make appropriate connections to and from it, rather than to and from the application delegate."

...在那之前,我只是认为我只是创建 NSWindowController 的子类,然后在 .xib 文件中声明窗口应该是此类的对象。现在我很困惑。 Controller 应该是什么类?它应该符合什么接口(interface)(对不起,协议(protocol))?如果它只是一个我指向导出的类,那么再一次 - 与我已有的 Delegate 有什么区别?

  • "Add a connection from the application delegate to the new controller object. When the application has finished launching, the application delegate should send a message to the controller to display the window."

...消息是什么?我发现的最接近的是 NSWindowController 类中的 showWindow: message ,但是当我写这个时:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

[trackController showWindow:self];

}

在AppDelegate的applicationDidFinishLaunching方法中,它显示错误“接收器类型'TrackController'例如消息是前向声明”,我只是无法理解它是什么意思。

不仅如此,如果这个窗口在空白项目中没有显示我创建的所有代码,为什么我应该发送一条消息来显示这个窗口?也许我应该以某种方式关闭此默认行为并自己创建此窗口的实例?

最佳答案

Controller 类的要点:在 Controller 和模型元素及其用户界面之间保持一定的比例是一个很好的做法。对于简单的应用程序,您可以将所有代码放在 AppDelegate 中。一旦您开发了更复杂的应用程序,您将需要从 AppDelegate 卸载代码。到不同的 Controller ,你将有不同的 View 来维护你的模型。我尝试在 View 和 Controller 之间保持 1:1 的关系,以使事情易于管理。

Controller 可以实现各种协议(protocol)来委托(delegate) UI 组件处理特定的功能。然后他们成为该组件的委托(delegate)。例如,在您看来,您有 NSTextView并且您想知道用户何时单击文本中的链接。然后你的 Controller 可以实现 <NSTextViewDelegate>并实现方法textView:clickedOnLink:atIndex: 。通过这样做,它成为您 NSTextView 的委托(delegate)。 .

Controller 应该是什么类?有多种 Controller 类可用于构建您的应用程序。我倾向于只使用 NSViewController 。该 Controller 能够处理 NSView 的实例显示在 NSWindow 中。 NSView支持的 View 层次结构允许您决定各种粒度场景(巨大、复杂的 Controller 或细粒度 - 这取决于您)。

有关前向声明问题,请参阅 this post .

我不明白你关于显示窗口的最后一个问题。显然,默认模板会生成一些管道代码来显示窗口。您可以使用该代码或自行编写。如果我没有正确理解你的意思,请澄清...

关于objective-c - cocoa 教程麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10937622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com