Objective-C iOS Development for Legacy & Enterprise Apps · 课时

UIView 与 UIViewController

了解 UIView 和 UIViewController 的生命周期与职责,它们是 iOS 界面的构建模块。

第 1 / 4 课11 个步骤

UIView 与 UIViewController 是 CoddyKit 上的免费 Objective-C iOS Development for Legacy & Enterprise Apps 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Objective-C iOS Development for Legacy & Enterprise Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Introduction to iOS UI

Welcome to building iOS interfaces! Every visual element you see in an iOS app, from buttons to entire screens, is built using two core components: UIView and UIViewController.

These are the fundamental building blocks that allow you to display content and manage user interactions in your Objective-C applications.

What is a UIView?

A UIView is the basic rectangular region that draws content and handles events on the screen. Think of it as a canvas where you can draw shapes, display text, or show images.

  • It's the base class for all visual elements.
  • It defines the content's position, size, and appearance.
  • It can contain other views, creating a view hierarchy.

Basic UIView Properties

UIViews have important properties that control their appearance and layout. Here are a few common ones:

  • frame: Defines the view's size and position relative to its superview.
  • bounds: Defines the view's size and position relative to its own coordinate system.
  • backgroundColor: Sets the view's background color.
  • alpha: Controls the view's transparency (0.0 is fully transparent, 1.0 is fully opaque).

Creating a UIView

You can create and configure a UIView programmatically. Here's how you might set up a simple red square view:

// In an Objective-C file (e.g., within a method)
CGRect viewFrame = CGRectMake(50, 50, 100, 100);
UIView *myView = [[UIView alloc] initWithFrame:viewFrame];
myView.backgroundColor = [UIColor redColor];
myView.alpha = 0.7;
// myView is now ready to be added to another view

What is a UIViewController?

A UIViewController is a crucial component that manages a hierarchy of views (often just one main view) and coordinates the flow of information between the views and your app's data model.

It acts as the brain for a specific screen or section of your app, handling user input, updating the view, and responding to system events.

UIViewController's Main View

Every UIViewController has a primary view property, which is an instance of UIView. This view acts as the root of the view hierarchy that the view controller manages.

You typically add all other UI elements (like buttons, labels, etc.) as subviews to this main view.

The `viewDidLoad` Lifecycle Method

UIViewControllers have a lifecycle with methods that are called at different stages. One of the most important is viewDidLoad.

  • It's called once after the controller's view has been loaded into memory.
  • This is the perfect place to perform initial setup for your views, like adding subviews, setting up constraints, or loading initial data.
  • It's called only once during the lifetime of the view controller.

Implementing `viewDidLoad`

Here's how you might override viewDidLoad in your custom view controller to add some initial UI setup:

// MyViewController.m
#import "MyViewController.h"

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad]; // Always call super!
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UILabel *welcomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
    welcomeLabel.text = @"Hello from MyViewController!";
    welcomeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:welcomeLabel];
}

@end

`viewWillAppear` and `viewDidAppear`

Besides viewDidLoad, two other common lifecycle methods relate to a view appearing on screen:

  • viewWillAppear:: Called just before the view controller's view is added to the view hierarchy and about to be displayed. Good for updating UI that might change frequently.
  • viewDidAppear:: Called after the view controller's view has been fully presented on screen. Good for starting animations or fetching data that requires the view to be visible.

Quick Check: UI Components

Which of the following statements best describes the primary responsibility of a UIViewController?

Recap: UIView & UIViewController

You've learned about the fundamental building blocks of iOS user interfaces!

  • UIView is the basic rectangular area for drawing and event handling.
  • UIViewController manages a view hierarchy and coordinates the interaction between views and data.
  • The view property of a UIViewController is the root of its managed UI.
  • Lifecycle methods like viewDidLoad, viewWillAppear, and viewDidAppear are key for setting up and updating your UI at different stages.

Next, we'll explore common UI controls and how to handle user events!

免费开始

用 AI 导师学习 Objective-C — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「UIView 与 UIViewController」课时是免费的吗?

是的 — 「UIView 与 UIViewController」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Objective-C iOS Development for Legacy & Enterprise Apps 课程的其余内容,请升级到 CoddyKit PRO。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

「UIView 与 UIViewController」这节课中我会学到什么?

了解 UIView 和 UIViewController 的生命周期与职责,它们是 iOS 界面的构建模块。 你通过在浏览器中直接运行的动手代码来练习 Objective-C iOS Development for Legacy & Enterprise Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Objective-C iOS Development for Legacy & Enterprise Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Objective-C iOS Development for Legacy & Enterprise Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「UIView 与 UIViewController」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Objective-C iOS Development for Legacy & Enterprise Apps 课中编写并运行代码吗?

能。每节 Objective-C iOS Development for Legacy & Enterprise Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. UIView 与 UIViewController
  2. 常用用户界面控件与事件
  3. 表格视图与集合视图
  4. 使用 NSLayoutConstraint 实现自动布局
← 返回 Objective-C iOS Development for Legacy & Enterprise Apps