3

xcode 的新手,当使用拖放在 XIB 中创建 UI 组件时,XCode 在哪里为这些组件声明和分配代码?

//where the heck are you?
UITextField* textField = [[UITextField alloc] initWithFrame: myFrame];

我检查了 XIB,但它只是 XML;检查了所有其他源文件,找不到它,所以我一定遗漏了一些东西。

我问是因为我需要在UITextField分配文本字段后向我的 , 添加一个委托。我尝试在 中执行此操作initWithNibName,但没有成功:

textField.delegate = self;
4

5 回答 5

3

我也面临同样的问题。如果我将此行放在 ViewDidLoad 方法中,它对我来说很好。试试这样。。

myTextField.delegate = self;
于 2013-09-10T05:53:04.533 回答
1

您可能会导入 UIKit 框架#import <UIKit/UIKit.h>。在 UIKit 中,所有的 UIElements 都被定义了。检查此Apple 文档

于 2013-09-10T04:58:46.477 回答
1

如果您为该元素创建一个 IBOutlet ,它不会工作吗?然后将委托分配给它?

于 2013-09-10T05:14:08.407 回答
1

对象加载过程:

当您使用 NSNib 或 NSBundle 的方法加载和实例化 nib 文件中的对象时,底层 nib 加载代码执行以下操作:

它将 nib 文件的内容和任何引用的资源文件加载到内存中:

==>整个 nib 对象图的原始数据被加载到内存中,但不是未归档的。

==>任何与 nib 文件关联的自定义图像资源都会被加载并添加到 Cocoa 图像缓存中;请参阅“关于图像和声音资源”。</p>

==>任何与 nib 文件关联的自定义声音资源都会被加载并添加到 Cocoa 声音缓存中;请参阅“关于图像和声音资源”。</p>

它取消归档 nib 对象图数据并实例化对象。它如何初始化每个新对象取决于对象的类型以及它在存档中的编码方式。nib 加载代码使用以下规则(按顺序)来确定要使用的初始化方法。

==>默认情况下,对象会收到一条initWithCoder:消息。在 OS X 中,标准对象列表包括视图、单元格、菜单和视图控制器,它们由系统提供并在默认 Xcode 库中可用。它还包括使用自定义插件添加到库中的任何第三方对象。即使您更改了此类对象的类,Xcode 也会将标准对象编码到 nib 文件中,然后在对象未归档时告诉归档器交换您的自定义类。

In iOS, any object that conforms to the NSCoding protocol is initialized using the initWithCoder: method. This includes all subclasses of UIView and UIViewController whether they are part of the default Xcode library or custom classes you define.

==>Custom views in OS X receive an initWithFrame: message. Custom views are subclasses of NSView for which Xcode does not have an available implementation. Typically, these are views that you define in your application and use to provide custom visual content. Custom views do not include standard system views (like NSSlider) that are part of the default library or part of an integrated third-party plug-in.

When it encounters a custom view, Xcode encodes a special NSCustomView object into your nib file. The custom view object includes the information it needs to build the real view subclass you specified. At load time, the NSCustomView object sends an alloc and initWithFrame: message to the real view class and then swaps the resulting view object in for itself. The net effect is that the real view object handles subsequent interactions during the nib-loading process.

Custom views in iOS do not use the initWithFrame: method for initialization.

==>Custom objects other than those described in the preceding steps receive an init message.

For detailed explanation you can read apple documentation: at here..

于 2013-09-10T07:45:49.190 回答
0

For that u need the include the the basic from frame work such as #import in ur . h file For example if you are using a text field then you need to declare property and map in your . h file as below

@property (nonatomic, retain) IBOutlet UITextField *txtNote;

for text field you need to add its delegate method too.

by using this you can map the text view once its done

in your .m file you need to declare @synthesize txtNote; below the @implementation

once its done there is no need to allocate anything but make sure that you release

menory once you leave from the page usig [txtNote release] in dealloc - (void)dealloc method

于 2013-09-10T12:26:40.453 回答