恢复文件文档基础应用程序
我的第一个基于文档的应用程序遇到了一些问题。很简单: 在 myDocument.m 上,我创建了一些方法
- (IBAction)salva:(id)sender {
[array addObject:@"Hello"];
[nomeLabel setStringValue:@"ciao"];
NSLog(@"%@",[array objectAtIndex:0]);
}
,用于在数组上保存值
- (BOOL) writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError {
return [array writeToURL:url atomically:YES];
}
,用于使用此方法将数组保存在文件上,
- (void) imposta {
[nomeLabel setStringValue:[array objectAtIndex:0]];
NSLog(@"Ciao");
}
我使用数组的内容设置标签的内容
- (BOOL) readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)outError{
[ array release];
array = [[NSMutableArray alloc] initWithContentsOfURL:url];
NSLog(@"%@",[array objectAtIndex:0]);
[self imposta];
return YES;
}
,用于加载文件。 问题是我无法使用加载数组的内容设置标签。该数组已加载,因为使用 NSLog 我看到了正确的值,问题是我无法将其放在上面
I have a little problem with my first document based application. Is very simple:
on myDocument.m I make some method
- (IBAction)salva:(id)sender {
[array addObject:@"Hello"];
[nomeLabel setStringValue:@"ciao"];
NSLog(@"%@",[array objectAtIndex:0]);
}
this for save a value on my array
- (BOOL) writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError {
return [array writeToURL:url atomically:YES];
}
this for save the array on a file
- (void) imposta {
[nomeLabel setStringValue:[array objectAtIndex:0]];
NSLog(@"Ciao");
}
with this method I set the content of a label with the content of array
- (BOOL) readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)outError{
[ array release];
array = [[NSMutableArray alloc] initWithContentsOfURL:url];
NSLog(@"%@",[array objectAtIndex:0]);
[self imposta];
return YES;
}
this for load the file.
The problem is that I can't set the label with the content of loaded array. The array wes loaded because with an NSLog I see the correct value, the problem is that I can't put it on th
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一次打开文档时,会在实例化窗口控制器之前调用
-readFromURL:ofType:error:
。这意味着您的 nameLabel 连接可能为零。您应该尽早在-awakeFromNib
或-windowControllerDidLoadNib:
中进行更新。但实际上,NSDocument 是一个模型对象,因此无论如何都不应该直接连接到视图对象。文档应该只存储其数据,窗口控制器应该负责更新视图。
When opening a document for the first time,
-readFromURL:ofType:error:
is called before the window controllers are instantiated. That means your nameLabel connection is probably nil. You should update in-awakeFromNib
or-windowControllerDidLoadNib:
at the very earliest.But really, NSDocument is a model object and therefore shouldn't be connected directly to a view object anyway. The document should just be storing its data, and the window controller should be responsible for updating the view.