简体   繁体   English

UITextView和UILabel子视图属性为零

[英]UITextView and UILabel Subview Properties are nil

In one of my view controllers, I've got the following structure: 在我的一个视图控制器中,我具有以下结构:

+-View
 +-Scroll View 1
  +-UIView
   +-Scroll View 2

...and I'm adding three subviews into Scroll View 2, one of which contains a series of UITextViews and UILabels as a form-like structure. ...并且我要在Scroll View 2中添加三个子视图,其中一个包含一系列UITextViews和UILabel,它们是类似表单的结构。

The problem I'm seeing is that when I try to access the properties that refer to those controls from the view controller level, they are coming up nil. 我看到的问题是,当我尝试从视图控制器级别访问引用这些控件的属性时,它们的状态为nil。 However, if I set a breakpoint in awakeFromNib inside the UIView that contains the UITextFields and UILabels, the properties are valid... it's almost like the reference disappears. 但是,如果我在包含UITextFields和UILabels的UIView的awakeFromNib中设置一个断点,则该属性有效...就像引用消失了一样。

I can clearly see that they exist, and if I do the following in the debug console after setting a breakpoint, I definitely get the view graph output to the console: 我可以清楚地看到它们的存在,如果在设置断点后在调试控制台中执行以下操作,我肯定会将视图图输出到控制台:

po [self.profileEditView recursiveDescription] 

Does this sound familiar to anyone, and if so, how can I remedy it? 这听起来对任何人都熟悉吗?如果是,我该如何补救?

One thing I'm thinking of doing is just not trying to access those properties directly, since the goal is to get the view controller to be the delegate of those UITextView controls so it can respond to becoming the first responder by adjusting Scroll View 1's position when each text view gets focus. 我想做的一件事就是不尝试直接访问那些属性,因为目标是使视图控制器成为那些UITextView控件的委托,以便它可以通过调整Scroll View 1的位置来响应成为第一响应者当每个文本视图获得焦点时。 It's just more of a hassle, but if that's what I have to do I'll go that route. 这只是一个麻烦,但是如果那是我必须要做的,我会走那条路线。

I've just never seen this happen before so I thought it was pretty strange. 我之前从未见过这种情况,所以我认为这很奇怪。 There's definitely a lot going on in this view controller. 这个视图控制器肯定发生了很多事情。

Additional Details: 额外细节:

  • The View Controller is defined in a Storyboard. 视图控制器在情节提要中定义。
  • The UIView subviews added at runtime have XIBs that I load programatically. 在运行时添加的UIView子视图具有我以编程方式加载的XIB。

I discovered what the problem is... I was inadvertently instantiating another subview as a subview inside initWithFrame (don't ask me why I was doing that... I think I was under the impression that I needed to do that to instantiate the XIB at that point), so the properties on the nested subview were valid but the properties of the top-level view (the one contained in Scroll View 2) were all nil. 我发现了问题所在...我无意中将另一个子视图实例化为initWithFrame内的子视图(不要问我为什么要这样做...我认为我的印象是我需要这样做以实例化此时是XIB),因此嵌套子视图上的属性是有效的,但顶层视图(滚动视图2中包含的视图)的属性全部为零。

I had a five-minute conversation with a developer in NZ and he asked about what I was doing there and it became readily apparent that what I needed to do was instantiate the view and its NIB in the view controller with: 我与NZ的开发人员进行了五分钟的交谈,他问我在那做什么,很明显我需要做的是在视图控制器中实例化视图及其NIB:

ProfileEditView *view = [[[NSBundle mainBundle] loadNibNamed:@"ProfileEditView" owner:nil options:nil] objectAtIndex:0];

...instead of instantiating it with initWithFrame. ...而不是使用initWithFrame实例化它。

Thanks for the help, however! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM