iPhone子视图设计(UIView vs UIViewController) [英] iPhone subview design (UIView vs UIViewController)

查看:141
本文介绍了iPhone子视图设计(UIView vs UIViewController)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个简单的测验应用程序。应用程序需要显示不同类型的QuizQuestions。每种类型的QuizQuestion都有不同的行为和UI。

I'm designing a simple Quiz application. The application needs to display different types of QuizQuestions. Each type of QuizQuestion has a distinct behavior and UI.

用户界面将是这样的:
替代文字http://dl.getdropbox.com/u/907284/Picture%201.png

The user interface will be something like this: alt text http://dl.getdropbox.com/u/907284/Picture%201.png

我希望能够在Interface Builder中设计每种类型的QuizQuestion。

I would like to be able to design each type of QuizQuestion in Interface Builder.

例如,MultipleChoiceQuizQuestion看起来像这个:
alt text http://dl.getdropbox.com/ u / 907284 / Picture%202.png

For example, a MultipleChoiceQuizQuestion would look like this: alt text http://dl.getdropbox.com/u/907284/Picture%202.png

最初,我打算让QuizQuestion类成为UIViewController。但是,我在Apple文档中读到UIViewControllers只应用于显示整个页面。

Originally, I planned to make the QuizQuestion class a UIViewController. However, I read in the Apple documentation that UIViewControllers should only be used to display an entire page.

因此,我制作了我的QuizController(管理整个屏幕,例如prev /下一个按钮)UIViewController和我的QuizQuestion类是UIView的子类。

Therefore, I made my QuizController (which manages the entire screen e.g. prev/next buttons) a UIViewController and my QuizQuestion class a subclass of UIView.

但是,要加载这个UIView(在IB中创建),我必须[1]在我的构造函数中执行以下操作:

However, to load this UIView (created in IB), I must[1] do the following in my constructor:

//MultipleQuizQuestion.m
+(id)createInstance {
    UIViewController *useless = [[UIViewController alloc] initWithNibName:@"MultipleQuizQuestion" bundle:nil];
    UIView *view = [[useless.view retain] autorelease];
    [useless release];
    return view; // probably has a memory leak or something
}

此类访问似乎不太可能是标准的还是面向对象的。这种类型的代码是正常/可接受的吗?或者我在设计的某处做了一个糟糕的选择?

This type of access does not seem to be standard or object-oriented. Is this type of code normal/acceptable? Or did I make a poor choice somewhere in my design?

谢谢你,

编辑(对于清晰度):我想有一个单独的类来控制multipleChoiceView ...就像一个ViewController,但显然只适用于整个窗口。也许我应该创建一个MultipleChoiceViewManager(而不是控制器!)并将文件的所有者设置为那个?

edit (for clarity): I'd like to have a separate class to control the multipleChoiceView...like a ViewController but apparently that's only for entire windows. Maybe I should make a MultipleChoiceViewManager (not controller!) and set the File's Owner to that instead?

推荐答案

你在正确的轨道上。在QuizController xib中,您可以通过将它们拖动到xib的主窗口而不是QuizController的主视图来创建单独的视图。然后,您可以根据问题类型设计所需的每个视图。当用户点击下一个或上一个时,删除上一个视图并使用视图控制器主视图上的-addSubview根据您的问题类型加载所需的视图,并跟踪当前显示的子视图。尝试这样的事情:

You're on the right track. In your QuizController xib, you can create separate views by dragging them to the xib's main window rather than to the QuizController's main view. Then you can design each view you need according to your question types. When the user taps next or previous, remove the previous view and load the view you need based on your question type using -addSubview on the view controller's main view and keep track of which subview is currently showing. Trying something like this:

[currentView removeFromSuperView];

switch(questionType)
{
    case kMultipleChoice:
        [[self view] addSubview:multipleChoiceView];
        currentView = multipleChoiceView;
        break;
    case kOpenEnded:
        [[self view] addSubview:openEndedView];
        currentView = openEndedView;
        break;
// etc.
}

其中multipleChoice视图和openEndedView是UIView出口在您的QuizController中连接到您在IB中设计的视图。在添加视图之前,您可能需要在父视图中混淆视图的位置以使其显示在正确的位置,但是您可以通过调用UIView上的-setBounds / -setFrame和/或-setCenter来执行此操作。

Where multipleChoice view and openEndedView are UIView outlets in your QuizController connected to the views you designed in IB. You may need to mess with the position of your view within the parent view before you add it to get it to display in the right place, but you can do this with calls to -setBounds/-setFrame and/or -setCenter on the UIView.

这篇关于iPhone子视图设计(UIView vs UIViewController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆