简体   繁体   English

ViewController无法在iOS 4.3上运行的ARC项目中从笔尖加载子视图?

[英]ViewController not loading subviews from nib in ARC project running on iOS 4.3?

My team has recently converted a fairly large project to ARC. 我的团队最近将一个相当大的项目转换为ARC。 The conversion went well and the app runs, and works fine on 5.0. 转换进行得很好,该应用程序可以运行,并且在5.0上运行良好。

The problem is on 4.3. 问题在4.3。 Any view controllers instantiated in code with plain init do not load any subviews of the vc's view (this does work on 5.0) 用普通init代码实例化的任何视图控制器都不会加载vc视图的任何子视图(这在5.0上有效)

Here's the summary: 这是摘要:

Instantiate a view controller with [[MyViewController alloc] init] . 使用[[MyViewController alloc] init]实例化视图控制器。 (UIViewController's init calls initWithNibName:nil bundle:nil) (UIViewController的init调用initWithNibName:nil bundle:nil)

This loads a nib with the same name as the View Controller (MyViewController.xib) 这将加载与视图控制器(MyViewController.xib)同名的笔尖

*Expected: the nib is instantiated normally with all subviews of top level view and all outlets set *预期:笔尖通常以顶级视图的所有子视图和所有插座设置实例化

*Actual: the nib is instantiated, and the MyViewController object's view property is set. *实际:笔尖已实例化,并设置了MyViewController对象的view属性。 However, the view's subviews array is empty and any outlets to these views are nil, and the view appears empty when presented 但是,该视图的subviews数组为空,并且这些视图的所有出口均为nil,并且在显示时该视图显得为空。

*Workaround: instantiate VC/load nib by using *解决方法:使用实例化VC / Load nib

[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil]

I haven't been able to replicate this in a clean project, either starting with ARC or converting to ARC. 从ARC开始或转换为ARC,我一直无法在一个干净的项目中复制它。 My team and I are working on this, but in the meantime, wondered if anyone else has run into this and found a root cause or trigger. 我和我的团队正在研究此问题,但与此同时,我想知道是否还有其他人遇到此问题并找到了根本原因或触发因素。

A follow up in case anyone else has this problem. 如果其他人有此问题,请进行跟进。

Since we are supporting 4.x and using ARC, we decided to use Mike Ash's MAZeroingWeakRef to get a weak pointer in 4.x. 由于我们支持4.x并使用ARC,因此我们决定使用Mike Ash的MAZeroingWeakRef来获取4.x中的弱指针。 Eventually we isolated the problem enough to trace it back to MAZeroingWeakRef. 最终,我们将问题隔离到足以追溯到MAZeroingWeakRef的水平。 We posted an issue to the MAZWR project and github and Mike explained what was happening: 我们向MAZWR项目发布了一个问题,github和Mike解释了发生了什么:

https://github.com/mikeash/MAZeroingWeakRef/issues/13 https://github.com/mikeash/MAZeroingWeakRef/issues/13

(Basically MAZWR changes the internal name of the VC class to something like TestViewController_MAZeroingWeakRefSubclass. The VC's init depends on this class name to load its associated nib -- so when it is changed it can't find the nib. By calling initWithNibName:bundle: and explicitly supplying the class name the associated nib is loaded properly.) (基本上,MAZWR将VC类的内部名称更改为类似TestViewController_MAZeroingWeakRefSubclass的类。VC的init依赖于该类名称来加载其关联的笔尖-因此,更改后它将找不到该笔尖。通过调用initWithNibName:bundle:并明确提供相关的笔尖已正确加载的类名。)

Not sure what's causing your problem, but if you create a common base class for all your view controllers, you can define the init method as: 不知道是什么引起了您的问题,但是如果您为所有视图控制器创建一个通用基类,则可以将init方法定义为:

 - (id)init
 {
     return [self initWithNibName:NSStringFromClass([self class]) bundle:nil];
 }

That way you can guarantee that it's loading the right nib without having to muddy up your code with typing explicit nib names everywhere. 这样,您可以保证它加载正确的笔尖,而不必在任何地方键入显式笔尖名称来弄乱您的代码。

I've sometimes had to use this trick with UITableViewControllers by the way, as their init method doesn't seem to follow the convention. 顺便说一下,有时我不得不对UITableViewControllers使用此技巧,因为它们的init方法似乎并不遵循约定。

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

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