superview和parentviewcontroller nil添加一个子视图后 [英] superview and parentviewcontroller nil after adding a subview

查看:145
本文介绍了superview和parentviewcontroller nil添加一个子视图后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我缺少一些基本的东西,所以我想问社群一些帮助。我正在基于一个基本的iPhone实用程序应用程序构建一个应用程序。我的MainView和FlipsideView共享一些元素,所以我已经为这些块创建了单独的ViewControllers和nib文件。为了做到这一点,我做了以下:
1.创建一个viewcontroller名为searchDateViewController,它是searchDateView.xib的文件的所有者
2. searchDateView.xib基本上是一个UIView里面的UILabel,视图已正确连接
3.在MainViewController.m和FlipsideViewController.m内部,我添加一个子视图如下:

I think I'm missing something fundamental and so I want to ask the community for some help. I'm building an app based around a basic iPhone Utility Application. My MainView and FlipsideView share some elements so I have created separate ViewControllers and nib files for those pieces. In order to do this I have done the following: 1. Created a viewcontroller called searchDateViewController which is the file's owner of searchDateView.xib 2. searchDateView.xib is basically a UIView with a UILabel inside, the view is wired up correctly 3. Inside both MainViewController.m and FlipsideViewController.m I add a subview as folllows:

- (void)loadView{
    [super loadView];
    searchDateViewController = [[SearchDateViewController alloc] initWithNibName:@"SearchDateView" bundle:nil];
    [[searchDateViewController view] setFrame:[searchDateView frame]];
    [[self view] addSubview:[searchDateViewController view]];

...
}

精细。基本上取决于在每个主视图和双侧视图中发生的动作,改变了nib的UILabel。但是,如果searchDateViewController从MainView或FlipsideView加载,我想做一些稍微不同的事情。但是,我似乎不知道哪个ViewController添加了searchDateViewController子视图。

Everything displays and works just fine. Basically depending on actions that happen in each of the main and flipside views the UILabel of the nib is changed. However, I wanted to do something slightly different if the searchDateViewController is loaded from the MainView or the FlipsideView. However, I can't seem to figure out which ViewController is adding the searchDateViewController subview.

在searchDateViewController中我尝试:

In searchDateViewController I tried:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"superview %@", self.view.superview);
    NSLog(@"parentviewcontroller %@", self.parentViewController);
}

在这两种情况下,我得到nil。

In both cases I get nil.

所以我的问题是 - 我可以找出哪个ViewController添加searchDateViewController aa subview?如果是这样怎么样?或者如果我的逻辑完全搞乱了,我应该怎么做呢?

So my question is - can I find out which ViewController is adding searchDateViewController a a subview? If so how? Or if my logic here is completely messed up, how should I be doing this?

谢谢!

推荐答案

viewDidLoad 。在你的case,这发生在这一行:

viewDidLoad is invoked when the view controller has loaded its view. In your case, that happends in this line:

[[searchDateViewController view] setFrame:[searchDateView frame]];

此时,您尚未调用 addSubview:

At that moment, you haven't yet called addSubview: so it is no wonder the view's superview is nil.

为了解决你的问题,你应该在SearchDateViewController中定义一个属性来区分不同的情况。这个属性将由创建SearchDateViewController实例的父控制器相应地设置。

To solve your problem, you should define a property inside SearchDateViewController to distinguish between the different cases. This property would then be set accordingly by the parent controller that creates the SearchDateViewController instance.

一般来说,我认为使用UIViewController子类不是一个好主意控制器,用作一个或多个全屏视图的子视图,而不是用作全屏视图本身。 UIViewController的大部分逻辑都是假设它用于管理全屏视图。例如,对于你的设计,我认为SearchDateViewController可能会修改视图的框架,当设备方向更改等。由于你不需要所有这个功能的非全屏子视图,我建议你直接子类化SearchDateViewController从NSObject 。

Generally, I do not think it is a good idea to use a UIViewController subclass as a controller for a view that is used as a subview of one or several fullscreen views rather than be used as a fullscreen view itself. Much of UIViewController's logic works on the assumption that it is used to manage a fullscreen view. For instance, with your design, I think it's possible that SearchDateViewController will modify the view's frame when the device orientation changes etc. Since you don't need all this functionality for a non-fullscreen subview, I suggest you subclass your SearchDateViewController directly from NSObject.

这篇关于superview和parentviewcontroller nil添加一个子视图后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆