Xcode:ViewController 不显示 .view 或 addSubview:方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7930260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:16:03  来源:igfitidea点击:

Xcode: ViewController shows no .view or addSubview: methods

xcodeuiviewcontrollermedia-playersubview

提问by DAS

I created two ViewControllers (Xcode 4 => Storyboard). After that I created two UIViewSubclasses, called PlayOutControlViewController.h and TVOutViewController.h.

我创建了两个 ViewControllers(Xcode 4 => Storyboard)。之后我创建了两个 UIViewSubclasses,称为 PlayOutControlViewController.h 和 TVOutViewController.h。

I want to display a video on an external Display, so I tried the following:

我想在外部显示器上显示视频,所以我尝试了以下操作:

(MediaPlayer.framework added, Changed the class of my UIViewSubclasses to PlayOut... and TVOut... - Controller.h)

(添加了 MediaPlayer.framework,将我的 UIViewSubclasses 的类更改为 PlayOut... 和 TVOut... - Controller.h)

PlayoutViewController.m

[TVOutViewController.view addSubview: MediaPlayer.view];
//Got Error: Property view not found on object of type TVOutViewController

[TVOutViewController addSubview: MediaPlayer.view]; //Got an Error
//Got Error: No known class method for selector addSubview

Sorry, I know what the Errors mean, but not how to fix them.

抱歉,我知道错误的含义,但不知道如何修复它们。

Thanks in advance!

提前致谢!

P.S. I'm in the PlayoutViewController.m, where:

PS 我在 PlayoutViewController.m 中,其中:

[self.view addSubview: (anyUIView)];
//works

//and

[PlayoutViewController.view addSubview: (anyUIView)];
//won't work.

... Thank you!

... 谢谢!

回答by hamstergene

A class and its instances are different things.

一个类和它的实例是不同的东西。

self.viewworks, because instances of class PlayoutViewController have viewproperty.

self.view有效,因为类 PlayoutViewController 的实例具有view属性。

PlayoutViewController.viewdoesn't, because the class object itself does not have this property. The same applies to TVOutViewController — you should call an instance, not the class itself.

PlayoutViewController.view没有,因为类对象本身没有这个属性。这同样适用于 TVOutViewController — 你应该调用一个实例,而不是类本身。

回答by DAS

Thanks so far.

到目前为止,谢谢。

My TVOutViewController is a subclass of UIViewController, as described here:

我的 TVOutViewController 是 UIViewController 的子类,如下所述:

@interface TVOutViewController : UIViewController {    }

I added a UIView to the TVOutViewController in InterfaceBuilder. Do I have to do something else to be able to do like:

我在 InterfaceBuilder 中向 TVOutViewController 添加了一个 UIView。我是否必须做其他事情才能做到:

[self.view addSubview: AnyUIView];

Second question:

第二个问题:

If I declare a method in TVOutViewController.m, like:

如果我在 TVOutViewController.m 中声明一个方法,例如:

+(void)addSubviewMethod:(id)sender { [self.view addSubview: AnyUIView]; }

Why am I not able to call it in the PlayoutControlViewController.m via

为什么我不能通过 PlayoutControlViewController.m 调用它

[TVOutViewController addSubviewMethod];

I added

我加了

#import "TVOutViewController.h";

to the PlayOutViewController.m file.

到 PlayOutViewController.m 文件。

Thank you all in advance!

谢谢大家!

OK SOLVED:

确定解决:

You were on the right track:

你在正确的轨道上:

I forgot to initialize the object. Now it works like a charm!

我忘了初始化对象。现在它就像一个魅力!

TVOutViewController *MoviePlayerView = [[TVOutViewController alloc] init];
[MoviePlayerView.view addSubview:moviePlayer.view];