Objective-C - 了解视图控制器

发布于 2024-12-02 19:26:10 字数 236 浏览 3 评论 0原文

我知道视图控制器有助于控制应用程序中的多个视图,但我很难理解何时使用它们。

如果我的应用程序包含一个主页、多个具有“层次结构”结构的视图以及一个与层次结构不相关的关于页面,那么我的应用程序应该包含哪些文件?应用程序委托、导航控制器和视图控制器?不止一个视图控制器?只是一个导航控制器吗?

另外,它们应该全部包含在一个 .xib 文件中,还是多个 .xib 文件中?

任何帮助将不胜感激。

谢谢。

I understand that view controllers help control multiple views in an application, but I have trouble understanding when to use them.

If I have an application with a main page, several views with a "hierarchy" structure, and an about page not connected with the hierarchy, what files should my application have? An appdelegate, navigation controller and view controller? More than one view controller? Just a navigation controller?

Also, should they all be contained in one .xib file, or multiple .xib files?

Any help would be greatly appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

违心° 2024-12-09 19:26:10

一个好习惯是为每个要显示的页面配备一个 UIViewController。如果我了解您的应用程序的结构,您应该有一个主页(其中包含许多其他 UIView)和另一个页面(关于页面)。如果这是真的,我建议使用两个 UIViewController。

UINavigationControllerUIViewController 的子类,可让您在页面之间“导航”。这并不是绝对必要的,但建议这样做(您也可以自己实现一个自定义导航系统,但利用 Apple 为您提供的系统更容易)。另一种导航系统是基于UITabBarController,如果你想看一下。

假设我了解您的应用程序的结构,您应该需要两个 .xib 文件,每个页面一个。

应用程序委托在概念上与视图控制器不同,您将只有一个由 Xcode 自动创建的应用程序委托(当然,您可以修改它以满足您的需要)。

A good habit is to have a UIViewController for each page you want to show. If I get the structure of your app you should have a main page (with many other UIViews inside it) and another page (about page). If that's true I suggest two UIViewControllers.

The UINavigationController is a subclass of UIViewController that lets you "navigate" among the pages. It's not strictly necessary but suggested (you can also implement your self a custom navigation system, but it's easier to exploit the one Apple offers you). Another navigation system is the one based on UITabBarController, if you want to take a look.

Assuming I get the structure of your app you should need two .xib file, one for each page you have.

The app delegate is conceptually different from a view controller, you'll have just a single app delegate, automatically created by Xcode (you can, of course, modify it to fit your needs).

煞人兵器 2024-12-09 19:26:10

每个“满屏内容”(苹果使用这个术语)应该由它的 UIViewController 或更可能是它的子类来处理。视点控制器用于处理视图出现或消失(进入/离开屏幕)、设备旋转、内存管理、导航到其他视图控制器等。如果您使用 IB 创建 UI,那么每个视图控制器很可能都有自己的 .xib 文件。

每个视图控制器都有一个视图(它是 view 属性),充当每个“满屏内容”的主视图,然后您可以向其中添加子视图。

UINavigationControllerUITabBarcontroller 可帮助您控制应用程序的层次结构。它们仅充当其他视图控制器的容器,并且不包含除导航栏或选项卡栏之外的任何 UI。使用选项卡栏控制器,您可以拥有多个视图控制器,其行为与浏览器选项卡完全相同。使用导航控制器,您可以拥有类似堆栈的导航,其中新的视图控制器从右到左推送,并在用户返回到上一个视图控制器时从左到右弹出。您甚至可以在标签栏控制器内的导航控制器内放置一个视图控制器。

如果您不想使用标签栏或导航控制器,则可以通过使用 presentModalViewController:animated: 以模态方式呈现视图控制器并通过 dismissModalViewControllerAnimated: 关闭来浏览视图控制器。如果您发送 YES 作为这些方法的动画参数,您将获得由视图控制器的 modalTransitionStyle 属性指定的呈现或消失的动画。可能的动画包括从底部滑入(默认)、整个屏幕的水平翻转、淡入/淡出和半页卷曲。

还有一些 Apple 提供的 UIViewController 子类可以帮助您更快地设置 UI,例如 UITableViewController ,它基本上是一个视图控制器,其中包含一个表作为主视图并符合“UITableView” code>dataSourceanddelegate` 协议需要定义每个单元的外观及其包含的内容。

在 iPad 上,有一个额外的容器控制器 UISplitViewController 以及使用 UIPopover 呈现新视图控制器的另一种方法。

Each "screenful of content" (Apple uses this term) should be handled by it's UIViewController or more likely a subclass of it. The point of view controller is to handle view appearing or disappearing (going on/offscreen), device rotation, memory management, navigating to other view controllers and so on. If you are creating your UI with IB, then each of those view controllers would most likely have it's own .xib file.

Each view controller has one view (it's view property) that acts as main view for each "screenful of content" to which you then add your subviews.

UINavigationController and UITabBarcontroller are there to help you control the hierarchy of your app. They only act as containers for other view controllers and don't contain any UI except navigation bar or tab bar. Using tab bar controller you can have multiple view controllers which act exactly like browser tabs. Using navigation controller you can have a stack-like navigation where new view controllers are pushed from right to left and are popped from left to right when user goes back to previous view controller. You can even have a view controller inside navigation controller inside a tab bar controller.

If you don't want to use tab bar or navigation controller, you can navigate through your view controllers by presenting them modally using presentModalViewController:animated: and dismissing by dismissModalViewControllerAnimated:. If you send YES for animated parameter of these methods, you will get an animation specified by the modalTransitionStyle property of view controller being presented or dismissed. Possible animations are slide in from bottom (default), horizontal flip of entire screen, fade in/out and half-page curl.

There are also some Apple-provided subclasses of UIViewController that help you setup your UI quicker like UITableViewController which is basically a view controller that contains a table as it's main view and conforms to 'UITableViewdataSourceanddelegate` protocols which are required to define how each cell looks and what it contains.

On iPad there is one additional container controller UISplitViewController and one additional way to present new view controllers using UIPopover.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文