Cocoa ViewController.m和Cocoa Touch ViewController.m [英] Cocoa ViewController.m vs. Cocoa Touch ViewController.m

查看:126
本文介绍了Cocoa ViewController.m和Cocoa Touch ViewController.m的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总新手问题如下...

Total newbie question follows...

我正在学习基于Objective C的Cocoa(OS X)应用程序开发。

I am learning Objective C based Cocoa (OS X) app development. Most of the books and videos I have are iOS based, so I'm seeing how the two platforms differ.

我已经将一个简单的iOS应用程序转换为OS X.我已经将一个简单的iOS应用程序转换为OS X。得到转换的OS X应用程序工作正常,但我有一个问题:为什么OS X ViewController.m文件有一个@interface部分?

I have converted a simple iOS app to OS X. I got the converted OS X app to work fine, but I have a question: Why doesn't OS X ViewController.m file have an @interface section?

我在助手编辑器中使用Interface Builder创建了iOS应用程序,将UI元素从Main.Storyboard拖动到ViewController.m。我将一个UILabel单元格拖到ViewController.m的@interface部分,创建了一个实例变量@property。在iOS中,ViewController.h public @interface部分保持为空。

I created the iOS app using Interface Builder, dragging UI elements from Main.Storyboard to ViewController.m, in the assistant editor. I dragged a UILabel cell into the @interface section of ViewController.m, created an instance variable @property. In iOS, the ViewController.h public @interface section remains empty.

在OS X应用程序中,我将NSTextField单元格拖放到ViewController.h的@interface部分文件创建一个类似的实例变量。

In the OS X app, I dragged an NSTextField cell to the @interface section of the ViewController.h file to create a similar instance variable.

OS X ViewController.h @interface部分是否正确放置IBOutlets?这不会暴露这些实例变量到其他类吗?在iOS的情况下,由于实例变量没有添加到ViewController.h文件中,我认为它们是隐藏的,正确的?我可以让OS X实例变量私有吗?

Is the OS X ViewController.h @interface section the correct place for IBOutlets? Doesn't this expose these instance variables to other classes? In the iOS case, since the instance variables were NOT added to the ViewController.h file, I assume they are hidden, correct? Can I make the OS X instance variables private?

如我所说,OS X代码工作,我只是想了解平台之间的差异,

As I mentioned, the OS X code works, I'm just trying to understand the differences between the platforms, and make sure I am headed in the right direction.

推荐答案

在我回答之前,我想检查一下是否发生在我身上 - 但是,当在新的iOS OR OS X应用程序中创建自定义 NSViewController 时,两者都会在实现中生成 @interface 档案(.m档案)

Before I answered I wanted to check if the same happened to me-- but when I created a custom NSViewController in a new iOS OR OS X app, both would generate the @interface in the implementation file (.m file)

所以我不知道为什么会看到这个。

So I'm not sure why you are seeing that.

(如你所说)在头文件(.h)中的 @interface 中声明的属性是public的。而在实现中 @interface 中声明的属性是私有的。这是因为你的实现中的 @interface 被称为扩展。这基本上只是一个匿名的类别。

However, to answer your question, (as you said) properties declared in the @interface in your header file (.h) are public. While properties declared in the @interface in the implementation are private. This is because the @interface inside of your implementation is called an "Extension". Which is basically just an anonymous "Category".

除非外部对象需要引用你的插件,否则我会继续将它们放在你的实现文件中。如果无论什么原因,不是为你生成的,手动输入你自己的类扩展来声明属性没有错误。

Unless outside objects need a reference to your outlets, then I would continue placing them inside of your implementation file. If for whatever reason one isn't generated for you, there is nothing wrong with manually typing out your own class extension to declare the properties

这篇关于Cocoa ViewController.m和Cocoa Touch ViewController.m的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆