简体   繁体   English

无法从UIViewController添加UIView

[英]can't add UIView from a UIViewController

I have some issue showing a view from a view controller. 我在显示来自视图控制器的视图时遇到了一些问题。 Basically, I have a UIViewController class, I programmatically created the view by overloading loadView method. 基本上,我有一个UIViewController类,我通过重载loadView方法以编程方式创建了视图。 Here is the code that I have: 这是我的代码:

UIView* view = controller.view;  
UIWindow* window = [UIApplication sharedApplication].keyWindow;  
if(!window)  
{  
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];  
}  
[window addSubview:view];

The view is not visible. 该视图不可见。

However, if I created a simple view based app by calling 但是,如果我通过调用创建了一个基于视图的简单应用程序

[self presentModalViewController: controller animated: YES];

Everything works well. 一切正常。

I am new to the iOS View Programming. 我是iOS View编程的新手。 Anything I did was wrong? 我做错了什么吗?

Steve, 史蒂夫

I'm not sure what you are trying to achieve here, but a typical iphone app will have a base UIViewController subclass, say a nav controller or tab controller and that controller's view is added as a subview of the UIWindow view in the appDelegate class. 我不确定您要在这里实现什么,但是典型的iphone应用程序将具有基本的UIViewController子类,例如nav控制器或Tab控制器,并且该控制器的视图将作为appDelegate类中UIWindow视图的子视图添加。

You then instantiate further UIViewControllers or subclasses thereof and those views get added as subviews of the current view and so on. 然后,您进一步实例化UIViewControllers或其子类,然后将这些视图添加为当前视图的子视图,依此类推。

Have you checked out the basic templates in XCode? 您是否签出了XCode中的基本模板? These should show you how the appDelegate and the like works. 这些应该向您展示appDelegate等的工作方式。 You generally don't need to mess with UIWindow in other classes, in my experience. 根据我的经验,通常不需要在其他类中与UIWindow混淆。

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

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