【问题标题】:injecting UINibExternalObjects to an self initialzied ViewController将 UINibExternalObjects 注入到自初始化的 ViewController
【发布时间】:2011-07-07 08:48:35
【问题描述】:

我想从一个 ViewController 导航到另一个。作为其中的一部分,我想 通过 ViewController 我想导航到一些信息。我封装了 信息到我想作为外部对象与目标挂钩的对象中 视图控制器。

我在 IB 中创建了外部对象,并为它提供了我在 NSDictionary 中引用的标识符,该标识符传递给 NibLoading 方法。

NSArray*    topLevelObjs = nil;
NSMutableDictionary* options = [[NSMutableDictionary alloc] initWithCapacity:1];
NSMutableDictionary* config = [[NSMutableDictionary alloc] initWithCapacity:1];

id detailImageVC = [[SelectedImageModalViewController alloc] init];
SelectedImageModalModel* selectImageModalModel = [[SelectedImageModalModel alloc] init];
selectImageModalModel.imageName = @"img@2x.png";

[config setValue:selectImageModalModel forKey:@"SelectImageModalModel"];
[options setValue:config forKey:UINibExternalObjects];

topLevelObjs = [[NSBundle mainBundle] loadNibNamed:@"SelectedImageModalViewController" owner:detailImageVC options:options];
if ([topLevelObjs count] == 0)
{
    NSLog(@"Warning! Could not substitute proxy objects in xib file.\n");
    return;
}

[appDelegate.navigationController presentModalViewController:detailImageVC animated:YES];

[options release];
[config release];
[selectImageModalModel release];
[detailImageVC release];

我的预期是,在我调用 presentModalViewController:animated: 之后,我会在与我的外部对象相同的 detailImageVC 上收到对 viewDidLoad 的调用 连接的。

相反,两者都没有发生。 viewWillApear: 将被调用,但 detailImageVC 不会保存我的外部引用。

感谢任何想法、意见或评论。谢谢!

【问题讨论】:

    标签: iphone ios nib viewcontroller viewdidload


    【解决方案1】:

    viewDidLoad 将在只有 ViewController 加载视图本身时被调用。

    例如; initWithNibName 不加载视图,它只是设置笔尖名称。当 ViewController 在未来某个时间点需要它的视图并且如果 ViewController.view 中没有视图,那么 ViewController 将像您一样加载视图,然后调用 viewDidLoad 本身。

    您的代码会加载 ViewController 本身的视图。所以你应该像这样在你的代码中调用viewDidLoad 方法:

    topLevelObjs = [[NSBundle mainBundle] loadNibNamed:@"SelectedImageModalViewController" owner:detailImageVC options:options];
    if (topLevelObjs.count == 0) {
        NSLog(@"Warning! Could not substitute proxy objects in xib file.\n");
        return;
    } else {
        [detailImageVC viewDidLoad];
    }
    

    如果您的 detailImageVC 不包含您的外部对象,那么您应该检查您的 nib 文件中的 IBOutlet 绑定和您的 SelectedImageModalViewController 中对应的 @property。如果属性不像 ARC 上的 @property(nonatomic, strong) 那样强大,或者像 @property(nonatomic, retain) 那样在非 ARC 上没有恢复,那么它在从 nib 唤醒后将不会保留您的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多