Xcode 4.2中的多个视图 [英] Multiple Views in Xcode 4.2
问题描述
我在找不到在没有故事板的情况下在Xcode 4.2中实现多个视图的教程时遇到了很多麻烦,这是一个类,所以我还不能使用storyboard。当我在主视图中点击一个按钮时,我只是试图找到一个UIPicker的第二个视图,我找不到这个版本的Xcode,它与旧版本的不同,让我感到困惑。
I'm having a lot of trouble finding a tutorial for implementing multiple views in Xcode 4.2 without storyboard, this is for a class so I can't use storyboard yet. I'm just trying to have a 2nd view with a UIPicker come up when a button is clicked in the main view, I just can't find one for this version of Xcode and it's different enough from the older versions to confuse me.
如果有人能够快速描述我需要做什么或者更新的教程我会很感激,感谢任何帮助:)
Any help appreciated if someone can give me a quick description of what I need to do this or a newer tutorial I'd appreciate it :)
推荐答案
我认为你应该阅读 UIView编程指南,以便很好地处理 UIView
的工作原理。我发现nibs / storyboard非常适合混淆新的iOS开发人员。
I think you should read the UIView Programming Guide to get a good handle on how UIView
s work exactly. I find nibs/storyboard are really great at confusing new iOS developers.
本质上, UIViewController
有一个你在 viewDidLoad中设置的视图
或 loadView
方法,使用 [self setView:someUIView]
。通过添加 UIView
s作为viewcontroller的Main视图的子视图,可以在屏幕上添加更多内容。例如
In essence, a UIViewController
has 1 view which you set in the viewDidLoad
or loadView
method by using the [self setView:someUIView]
. You add more stuff to the screen by adding UIView
s as a subview of the viewcontroller's "Main" view. For example
-(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子类的外观:
Now here we have our viewController who'se view is just a plain UIView which in turn has 2 subviews; newButton and myView. Since we created the MyView class, maybe it contains subviews as well! Let's take a look at what a UIView subclass could look like:
// 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个按钮!但视图结构是树:
So in this example we would have a view controller who'se view now contains 2 button! But the view structure is a tree:
mainView
/ \
newButton myView
\
newButton2
如果您有任何其他问题,请与我们联系!
Let me know if you have any other questions!
马特
这篇关于Xcode 4.2中的多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!