[英]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.h
和MainView.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.h
和MainView.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.