xcode 如何在 Interface builder .xib 文件中实例化 NSObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/950587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to instantiate an NSObject in the Interface builder .xib file
提问by Yohann T.
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:这是我的第一篇文章,放纵一下。
回答by Chris Suter
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.
您还应该确保您了解什么是视图、控制器和模型对象。这方面的文献很多。
回答by MystikSpiral
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 的文件所有者实现扩展类。从那时起,只需将实例变量添加到正方形的扩展类中即可。该过程的最后一步是将方法添加到您的类中,以将每个控件绑定到操作。
- (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 的别名,但作为对接口生成器的提示)然后你可以通过 File's Owner 将你的控件绑定到方法(使用控件 + 拖动来建立关联)。
Good Luck!
祝你好运!
回答by Yohann T.
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:
为了使用 square 对象,我只需要引用头文件:
#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 中,我只需要在做任何其他事情之前分配/初始化 square 对象:
(IBAction)myInit
{
square = [[Square alloc] init];
}
Now I can use the methods of the first post And Voila !
现在我可以使用第一篇文章的方法了,瞧!
ps: critics, fire up!
ps:影评人,火起来!