如何在Interface构建器.xib文件中实例化NSObject

时间:2021-04-09 19:58:16

Greetings,

I have a view based application project where I have created an NSObject class called "SquareClass". Now, from the Xcode's interface builder, I want to be able to instantiate that "SquareClass" into a square object with global scope, in such a way that, when I create actions from any UI controls(i.e textboxes, buttons etc...), I want to be able to call methods of that object within those actions.

我有一个基于视图的应用程序项目,我在其中创建了一个名为“SquareClass”的NSObject类。现在,从Xcode的界面构建器,我希望能够将“SquareClass”实例化为具有全局范围的方形对象,这样,当我从任何UI控件(即文本框,按钮等)创建动作时... ),我希望能够在这些操作中调用该对象的方法。

example:

(void)MyAction1:(id)color
{
   [square setColor:color];
}

(void)MyAction2:(id)width
{
   [square setWidth:width];
}

As you can see, the square object needs to have a global scope. This might seems easy or maybe the wrong way of doing it for some of us. I've look through the web, and kinda found a way for a .nib file, not for a .xib file.

如您所见,方形对象需要具有全局范围。对于我们中的一些人来说,这似乎很容易或者可能是错误的方式。我浏览了网页,有点找到.nib文件的方法,而不是.xib文件。

Any suggestion would be greatly appreciated. Thanks a lot.

任何建议将不胜感激。非常感谢。

Yohann.

ps: This is my first post EVER, be indulgent.

ps:这是我的第一篇文章,是放纵的。

3 个解决方案

#1


Firstly, a NIB file is, for all intents and purposes, the same as an XIB file; it’s just a compiled version of a XIB file.

首先,对于所有意图和目的,NIB文件与XIB文件相同;它只是一个XIB文件的编译版本。

Now, to create an object derived from NSObject in your NIB/XIB, simply drag an NSObject from the library window into your file. Then on the Identity Inspector change the class to your custom class.

现在,要在NIB / XIB中创建从NSObject派生的对象,只需将NSObject从库窗口拖到文件中即可。然后在Identity Inspector上将类更改为您的自定义类。

If you define methods like this:

如果你定义这样的方法:

- (IBAction)myAction:(id)sender

you’ll be able to hook them up in Interface Builder.

您将能够在Interface Builder中将它们连接起来。

Having said all that, it’s difficult to tell whether what you’re doing is the right thing to do at all. It sounds to me like you should be using an NSViewController subclass so have a look at the documentation for that.

说了这么多,很难说你正在做什么是正确的做法。听起来像你应该使用NSViewController子类,所以看看文档。

You should also make sure you understand what view, controller and model objects are. There’s plenty of literature on this.

您还应该确保了解视图,控制器和模型对象是什么。有很多关于此的文献。

#2


If you create a class extending UIView, then you can make the File's Owner for your xib implement the extended class. From that point simply add an instance variable to your extended class for the square. The last step in the process is to add methods to your class to bind to each of the controls to actions.

如果您创建了一个扩展UIView的类,那么您可以使xib的File's Owner实现扩展类。从那时起,只需将实例变量添加到广场的扩展类中。该过程的最后一步是向类添加方法以将每个控件绑定到操作。

- (IBAction) pinSelected: (id)sender;

Once you create these methods (be sure to make them return IBAction, it is actually an alias for void but acts as a hint to INterface Builder) then you can bind your controls to the method through File's Owner (using control + drag to establish the link).

一旦你创建了这些方法(确保它们返回IBAction,它实际上是void的别名但作为INterface Builder的提示)然后你可以通过File的Owner将你的控件绑定到方法(使用control + drag来建立链接)。

Good Luck!

#3


I found how to do what I needed which is NOT done within the XIB file. It is way simpler than what i thought, eventhough the above suggestions are VALID.

我发现如何做我需要的,而不是在XIB文件中完成的。它比我想象的要简单,尽管上面的建议是有效的。

In order to use the square object, I just had to reference the header file:

为了使用方形对象,我只需要引用头文件:

 #import "square.h"

in the SquareUIViewController.h and add a reference to a square object:

在SquareUIViewController.h中添加对方形对象的引用:

Square *square;

Then in the SquareUIController.m, I just have to allocate/initialize the square object prior doing anything else:

然后在SquareUIController.m中,我只需要在执行任何其他操作之前分配/初始化方形对象:

(IBAction)myInit
{
square = [[Square alloc] init];
}

Now I can use the methods of the first post And Voila !

现在我可以使用第一篇文章和Voila的方法!

ps: critics, fire up!

ps:评论家,火了!

#1


Firstly, a NIB file is, for all intents and purposes, the same as an XIB file; it’s just a compiled version of a XIB file.

首先,对于所有意图和目的,NIB文件与XIB文件相同;它只是一个XIB文件的编译版本。

Now, to create an object derived from NSObject in your NIB/XIB, simply drag an NSObject from the library window into your file. Then on the Identity Inspector change the class to your custom class.

现在,要在NIB / XIB中创建从NSObject派生的对象,只需将NSObject从库窗口拖到文件中即可。然后在Identity Inspector上将类更改为您的自定义类。

If you define methods like this:

如果你定义这样的方法:

- (IBAction)myAction:(id)sender

you’ll be able to hook them up in Interface Builder.

您将能够在Interface Builder中将它们连接起来。

Having said all that, it’s difficult to tell whether what you’re doing is the right thing to do at all. It sounds to me like you should be using an NSViewController subclass so have a look at the documentation for that.

说了这么多,很难说你正在做什么是正确的做法。听起来像你应该使用NSViewController子类,所以看看文档。

You should also make sure you understand what view, controller and model objects are. There’s plenty of literature on this.

您还应该确保了解视图,控制器和模型对象是什么。有很多关于此的文献。

#2


If you create a class extending UIView, then you can make the File's Owner for your xib implement the extended class. From that point simply add an instance variable to your extended class for the square. The last step in the process is to add methods to your class to bind to each of the controls to actions.

如果您创建了一个扩展UIView的类,那么您可以使xib的File's Owner实现扩展类。从那时起,只需将实例变量添加到广场的扩展类中。该过程的最后一步是向类添加方法以将每个控件绑定到操作。

- (IBAction) pinSelected: (id)sender;

Once you create these methods (be sure to make them return IBAction, it is actually an alias for void but acts as a hint to INterface Builder) then you can bind your controls to the method through File's Owner (using control + drag to establish the link).

一旦你创建了这些方法(确保它们返回IBAction,它实际上是void的别名但作为INterface Builder的提示)然后你可以通过File的Owner将你的控件绑定到方法(使用control + drag来建立链接)。

Good Luck!

#3


I found how to do what I needed which is NOT done within the XIB file. It is way simpler than what i thought, eventhough the above suggestions are VALID.

我发现如何做我需要的,而不是在XIB文件中完成的。它比我想象的要简单,尽管上面的建议是有效的。

In order to use the square object, I just had to reference the header file:

为了使用方形对象,我只需要引用头文件:

 #import "square.h"

in the SquareUIViewController.h and add a reference to a square object:

在SquareUIViewController.h中添加对方形对象的引用:

Square *square;

Then in the SquareUIController.m, I just have to allocate/initialize the square object prior doing anything else:

然后在SquareUIController.m中,我只需要在执行任何其他操作之前分配/初始化方形对象:

(IBAction)myInit
{
square = [[Square alloc] init];
}

Now I can use the methods of the first post And Voila !

现在我可以使用第一篇文章和Voila的方法!

ps: critics, fire up!

ps:评论家,火了!