简体   繁体   English

superview和parentviewcontroller在添加子视图后为零

[英]superview and parentviewcontroller nil after adding a subview

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. 我正在构建一个基于iPhone实用程序基本应用程序的应用程序。 My MainView and FlipsideView share some elements so I have created separate ViewControllers and nib files for those pieces. 我的MainView和FlipsideView共享一些元素,所以我为这些片段创建了单独的ViewControllers和nib文件。 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: 为了做到这一点,我做了以下工作:1。创建了一个名为searchDateViewController的viewcontroller,它是searchDateView.xib的文件所有者2. searchDateView.xib基本上是一个内部有UILabel的UIView,视图连接正确3.里面MainViewController.m和FlipsideViewController.m我将子视图添加为folllows:

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

...
}

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. 基本上取决于每个主视图和翻转视图中发生的动作,笔尖的UILabel被更改。 However, I wanted to do something slightly different if the searchDateViewController is loaded from the MainView or the FlipsideView. 但是,如果从MainView或FlipsideView加载searchDateViewController,我想做一些稍微不同的事情。 However, I can't seem to figure out which ViewController is adding the searchDateViewController subview. 但是,我似乎无法弄清楚哪个ViewController正在添加searchDateViewController子视图。

In searchDateViewController I tried: 在searchDateViewController中,我试过:

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

In both cases I get nil. 在这两种情况下,我都没有。

So my question is - can I find out which ViewController is adding searchDateViewController aa subview? 所以我的问题是 - 我能找出哪个ViewController正在添加searchDateViewController一个子视图? If so how? 如果是这样的话? Or if my logic here is completely messed up, how should I be doing this? 或者,如果我的逻辑完全搞砸了,我该怎么做呢?

Thanks! 谢谢!

viewDidLoad is invoked when the view controller has loaded its view. 视图控制器加载其视图时调用viewDidLoad In your case, that happends in this line: 在你的情况下,发生在这一行:

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

At that moment, you haven't yet called addSubview: so it is no wonder the view's superview is nil. 那时,你还没有调用addSubview:所以难怪视图的superview是零。

To solve your problem, you should define a property inside SearchDateViewController to distinguish between the different cases. 要解决您的问题,您应该在SearchDateViewController中定义一个属性,以区分不同的情况。 This property would then be set accordingly by the parent controller that creates the SearchDateViewController instance. 然后,由创建SearchDateViewController实例的父控制器相应地设置此属性。

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. 通常,我认为使用UIViewController子类作为视图的控制器不是一个好主意,该视图用作一个或多个全屏视图的子视图,而不是用作全屏视图本身。 Much of UIViewController's logic works on the assumption that it is used to manage a fullscreen view. UIViewController的大部分逻辑都假设它用于管理全屏视图。 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. 例如,根据您的设计,我认为当设备方向改变等时,SearchDateViewController可能会修改视图的框架。由于您不需要非全屏子视图的所有这些功能,我建议您直接从NSObject继承您的SearchDateViewController 。

ViewController and views are completely separate. ViewController和视图是完全独立的。

In most cases, when you add a subview to a parent view you don't add its controller to the parent's viewController. 在大多数情况下,当您将子视图添加到父视图时,不会将其控制器添加到父视图中。 The exception to this rule is the navigation controller which adds the controller instead of the view to maintain a hierarchy of view controllers. 此规则的例外是导航控制器,它添加控制器而不是视图以维护视图控制器的层次结构。

Your SearchDate viewController can't find a parent controller because you never assigned one and the system does not do it automatically. 您的SearchDate viewController找不到父控制器,因为您从未分配过父控制器,系统不会自动执行。 You can just assign a parent controller when you evoke the view from another controller. 当您从另一个控制器唤起视图时,您可以只分配一个父控制器。

searchDateViewController.parentController=self;

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

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