添加子视图后 superview 和parentviewcontroller nil

发布于 2024-08-18 05:24:36 字数 1237 浏览 8 评论 0原文

我认为我错过了一些基本的东西,所以我想向社区寻求一些帮助。我正在构建一个基于基本 iPhone 实用程序应用程序的应用程序。我的 MainView 和 FlipsideView 共享一些元素,因此我为这些部分创建了单独的 ViewController 和 nib 文件。为了做到这一点,我做了以下工作: 1.创建一个名为searchDateViewController的视图控制器,它是searchDateView.xib文件的所有者 2. searchDateView.xib 基本上是一个带有 UILabel 的 UIView,视图连接正确 3. 在 MainViewController.m 和 FlipsideViewController.m 中,我添加一个子视图,如下所示:

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

...
}

一切都显示并正常工作。基本上,根据主视图和反面视图中发生的操作,笔尖的 UILabel 会发生变化。但是,如果 searchDateViewController 是从 MainView 或 FlipsideView 加载的,我想做一些稍微不同的事情。但是,我似乎无法弄清楚哪个 ViewController 正在添加 searchDateViewController 子视图。

在 searchDateViewController 中我尝试过:

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

在这两种情况下我都得到零。

所以我的问题是 - 我能找出哪个 ViewController 正在添加 searchDateViewController 子视图吗?如果是这样怎么办?或者如果我这里的逻辑完全混乱,我应该怎么做?

谢谢!

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]];

...
}

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.

In searchDateViewController I tried:

- (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 a a subview? If so how? Or if my logic here is completely messed up, how should I be doing this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-08-25 05:24:36

viewDidLoad 在视图控制器加载其视图时被调用。在你的例子中,这发生在这一行:

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

此时,你还没有调用 addSubview: ,所以难怪视图的超级视图为零。

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

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

viewDidLoad is invoked when the view controller has loaded its view. 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.

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.

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.

一紙繁鸢 2024-08-25 05:24:36

ViewController 和视图是完全分开的。

在大多数情况下,当您将子视图添加到父视图时,您不会将其控制器添加到父视图的 viewController 中。此规则的例外是导航控制器,它添加控制器而不是视图来维护视图控制器的层次结构。

您的 SearchDate viewController 无法找到父控制器,因为您从未分配过父控制器,并且系统不会自动执行此操作。当您从另一个控制器调用视图时,您只需分配一个父控制器即可。

searchDateViewController.parentController=self;

ViewController and views are completely separate.

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. You can just assign a parent controller when you evoke the view from another controller.

searchDateViewController.parentController=self;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文