Cocoa没有Interface Builder,初始化一个app controller的实例? [英] Cocoa without Interface Builder, initialize an instance of app controller?

查看:158
本文介绍了Cocoa没有Interface Builder,初始化一个app controller的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要计划编写没有IB的应用程序,我正在尝试学习更多关于编程的过程。

I don't plan to write applications without IB, I'm just in the process of trying to learn more about programming.

如何在启动时获取我的AppController类的单个实例?你可以清除 + initialize -init 的使用吗?如果我明白, + initialize 在启动时调用所有类。我如何使用这个来创建一个实例的我的AppController与实例变量组成我的界面?

How can I get a single instance of my AppController class at startup? (It's normally loaded from the nib.) And can you clear up the use of +initialize and -init? If I understand, +initialize is called on all classes at startup. How can I use this to create an instance of my AppController with instance variables that make up my interface?

希望有意义,并感谢任何帮助。

Hope that makes sense, and thanks for any help.

推荐答案

+ initalize 子类首次接收消息。所以,当你做:

+initalize is sent to a class the first time it or one of its subclasses receives a message for the first time. So, when you do:

instance = [[[YourClass alloc] init] autorelease];

alloc 消息触发器 initialize

如果你对子类做同样的事情:

If you do the same thing with a subclass:

instance = [[[SubclassOfYourClass alloc] init] autorelease];

alloc $ c> + [YourClass initialize] 与其他人做的一样(在触发 + [SubclassOfYourClass initialize] 每个类的 initialize 永远不会被调用多次(除非你自己用 [super initialize] [SomeClass initialize] - 不要这样做,因为方法不会期望它。)

That alloc message will trigger +[YourClass initialize] the same way the other one did (prior to also triggering +[SubclassOfYourClass initialize]. But only one of these will do it—each class's initialize never gets called more than once. (Unless you call it yourself with [super initialize] or [SomeClass initialize]—so don't do that, because the method won't be expecting it.)

-init ,另一方面,初始化一个新的实例在表达式 [[YourClass alloc] init] ,你直接将消息直接发送到实例,你也可以通过另一个初始化器( [[YourClass alloc] initWithSomethingElse:bar] )一个便利工厂( [YourClass instance] )。

-init, on the other hand, initializes a new instance. In the expression [[YourClass alloc] init], you are personally sending the message directly to the instance. You may also call it indirectly, through another initializer ([[YourClass alloc] initWithSomethingElse:bar]) or a convenience factory ([YourClass instance]).

不像 initialize ,你应该总是发送 init (或者另一个初始化器,如果适用)到你的超类。大多数init方法看起来大致如下:

Unlike initialize, you should always send init (or another initializer, if appropriate) to your superclass. Most init methods look roughly like this:

- (id) init {
    if ((self = [super init])) {
        framistan = [[Framistan alloc] init];
    }
    return self;
}

详细信息不同(此方法或超类或两者都可以接受参数,人们喜欢 self = [super init] 在自己的行, Wil Shipley不分配给 self 所有),但基本思想是一样的:call [super init [WithSomething:...]] ,确保它没有返回 nil ,设置实例

Details differ (this method or the superclass's or both may take arguments, and some people prefer self = [super init] on its own line, and Wil Shipley doesn't assign to self at all), but the basic idea is the same: call [super init[WithSomething:…]], make sure it didn't return nil, set up the instance if it didn't, and return whatever the superclass returned.



This implies that you can return nil from init, and indeed you can. If you do this, you should [self release], so that you don't leak the failed object. (For detecting invalid argument values, an alternative is NSParameterAssert, which throws an exception if the assertion fails. The relative merits of each are beyond the scope of this question.)


如何使用这个来创建一个AppController的实例,其中包含构成我的接口的实例变量?

How can I use this to create an instance of my AppController with instance variables that make up my interface?

最好的方法是在 main 中执行所有操作:

The best way is to do it all in main:

int main(int argc, char **argv) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    AppController *controller = [[[AppController alloc] init] autorelease];
    [[NSApplication sharedApplication] setDelegate:controller]; //Assuming you want it as your app delegate, which is likely
    int status = NSApplicationMain(argc, argv);

    [pool drain];
    return status;
}

您将在您的应用程序委托方法中执行任何其他设置 AppController

You'll do any other set-up in your application delegate methods in AppController.

您已经知道了这一点,但对于任何读过这个的人: Nibs是你的朋友。 Interface Builder是您的朋友。不要与框架工作,使用它,并以图形方式构建你的界面,你的应用程序会更好。

You already know this, but for anyone else who reads this: Nibs are your friend. Interface Builder is your friend. Don't fight the framework—work with it, and build your interface graphically, and your application will be better for it.

这篇关于Cocoa没有Interface Builder,初始化一个app controller的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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