[英]Basic Objective-C: Why does this code work?
问题1)
我有一个基于导航的应用程序,在其中单击条目可打开包含该条目详细信息的第二个视图。
现在可以使用以下代码
[self.navigationController pushViewController:descriptionController animated:YES];
[self.descriptionController.textView setText:[s description]];
self.descriptionController.title = [s name];
但是,如果我切换了第一条和第三条语句,那么当我第一次单击某个项目时,详细信息视图将显示Lorem Ipsum文本。 第一次之后,它会按预期工作。 我认为这样做会更正确。 为什么要在第一个视图中显示Lorem Ipsum,但之后却不显示任何内容?
问题2)
另外,我的textView元素是详细信息视图的一部分。 在我的视图控制器类中,我有:
@property( nonatomic, retain) IBOutlet UITextView *textView;
但是如果我将其更改为
@property( nonatomic, copy) IBOutlet UITextView *textView;
然后它崩溃了。 我不认为它会崩溃,但我认为它只是创建新实例而已,什么也没占用。 有人可以解释吗?
问题是-[UIViewController视图]及其所有子视图都是从NIB或在loadView中以编程方式延迟填充的。 所以:
//At this point descriptionController.view == nil && textView == nil
[self.navigationController pushViewController:descriptionController animated:YES];
//At this point descriptionController.view and textView are set
[self.descriptionController.textView setText:[s description]];
self.descriptionController.title = [s name];
因此,如果您在其他行之后推动交换,则推动(将加载视图)
[self.descriptionController.textView setText:[s description]];
self.descriptionController.title = [s name];
//At this point descriptionController.view == nil && textView == nil
[self.navigationController pushViewController:descriptionController animated:YES];
//At this point descriptionController.view and textView are set
您正在将设置消息发送到nil值(不执行任何操作),然后使用其默认值加载视图。
在我的视图控制器类中,我有:
@property( nonatomic, retain) IBOutlet UITextView *textView;
但是如果我将其更改为
@property( nonatomic, copy) IBOutlet UITextView *textView;
然后它崩溃了。
因为UITextView不符合NSCopying。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.