Я думаю, вы должны прочитать Руководство по программированию UIView , чтобы получить представление о том, как UIView
работает точно.Я считаю, что перья / раскадровки действительно хороши, чтобы сбить с толку новых разработчиков iOS.
По сути, UIViewController
имеет 1 представление, которое вы устанавливаете в методе viewDidLoad
или loadView
с помощью [self setView:someUIView]
.Вы добавляете больше материала на экран, добавляя UIView
s как подпредставление основного представления viewcontroller.Например,
-(void)loadView {
// Create a view for you view controller
UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self setView:mainView];
// Now we have a view taking up the whole screen and we can add stuff to it
// Let's try a button, a UIButton is a subview of UIView
UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// We need to set a frame for any view we add so that we specify where it should
// be located and how big it should be!
[newButton setFrame:CGRectMake(x,y,width,height)];
// Now let's add it to our view controller's view
[self.view addSubview:newButton];
// You can do the same with any UIView subclasses you make!
MyView *myView = [[MyView alloc] init];
[myView setFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:myView];
}
Теперь у нас есть viewController, чье представление является простым UIView, у которого, в свою очередь, есть 2 подпредставления;newButton и myView.Поскольку мы создали класс MyView, возможно, он также содержит подпредставления!Давайте посмотрим, как может выглядеть подкласс UIView:
// Here is the init method for our UIView subclass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Let's add a button to our view
UIButton *newButton2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// Of course, the frame is in reference to this view
[newButton2 setFrame:CGRectMake(x,y,width,height)];
// We add just using self NOT self.view because here, we are the view!
[self addSubview:newButton2];
}
return self;
}
Так что в этом примере у нас будет контроллер представления, вид которого теперь содержит 2 кнопки!Но структура представления представляет собой дерево:
mainView
/ \
newButton myView
\
newButton2
Дайте мне знать, если у вас есть другие вопросы!
Matt