简体   繁体   English

在iOS应用中,即使没有添加主自定义UIView,我仍然可以将其他视图对象添加为子视图吗?

[英]In an iOS app, even when no main custom UIView is added, I can still add other view objects as subview?

It is quite strange that in some other iOS apps, I created a Single View app, and then added MainView.h and MainView.m that is the subclass of UIView , but then in another program, I didn't add such class, but could immediately do: 奇怪的是,在其他一些iOS应用程序中,我创建了一个Single View应用程序,然后添加了MainView.hMainView.m这是UIView的子类),但是在另一个程序中,我没有添加此类,但是可以立即做:

ShapeView *sv;

sv = [[ShapeView alloc] initWithFrame:CGRectMake(10, 170, 140, 140)];
[self.view addSubview:sv];

inside of ViewController.m . ViewController.m内部。 Of course, ShapeView.m has to implement drawRect to properly draw the shape. 当然, ShapeView.m必须实现drawRect才能正确绘制形状。 But how come we don't need to add a MainView.h and MainView.m first this time and still draw on some main view? 但是为什么我们这次不需要先添加MainView.hMainView.m而仍然使用某些主视图呢?

You don't have to add MainView.h and .m for any app. 您无需为任何应用添加MainView.h.m That is necessary only when you want your own customized UIView named "MainView". 仅当您需要自己的名为“ MainView”的自定义UIView时才需要这样做。 If you don't need to draw the view or part of the view yourself, view controller + XIB/NIB work well. 如果您不需要自己绘制视图或视图的一部分,则视图控制器+ XIB / NIB可以很好地工作。

The main UIViewController in your app (which is created from Single View App template as you mentioned) creates a default UIView which is loaded from the default NIB/XIB. 应用程序中的主UIViewController (如前所述是从Single View App模板创建的)会创建一个默认UIView ,该UIView是从默认NIB / XIB加载的。 It is available as self.view in your UIViewController code after the view is loaded. 加载视图后,可以在UIViewController代码中以self.view形式使用它。

In fact MainView is nothing special compared with ShapeView . 实际上,与ShapeView相比, MainView没什么特别的。 They are both customized UIView subclasses. 它们都是自定义的UIView子类。

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

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