What is the difference between ViewController and ContentView?

Asked 2 years ago, Updated 2 years ago, 38 views

I use ContentView in my reference book, but when I go through it, I see only ViewController articles.
What is the difference between these?

swift

2022-09-30 20:12

1 Answers

I felt that the question was a little too vague, but I don't think the answer will be explosive, so I'm going to answer it.

If you are currently building apps on iOS (or other Apple platforms), there are two main ways to design a screen (or UI design).

UIKit (Other than iOS, the term is different, but details are omitted)

Create and edit files with the extension storyboard or xib with a special editor (called Interface Builder) that allows you to make graphical edits.

This is a classic method that has been in use since the last century, and you can also use Objective-C, also a classic language, instead of Swift.

For iOS applications, a class called ViewController that inherits the class UIViewController is provided in the standard template, so you will be describing non-screen design operations and processing in the class

.

As the app grows a little larger and involves multiple screens, you will need many classes other than ViewController and you can use a name other than ViewController.

SwiftUI

We design the screen by describing the screen configuration as text in a file called .swift, the source file of the programming language Swift.

This is a new method that emerged in the 2019 WWDC and is dedicated to Swift languages."I use ContentView for reference books" means that reference books are about SwiftUI.

When you create a SwiftUI project in Xcode, you create a structure called ContentView that matches View, so you have to edit this ContentView to describe both screen design and operation.

As with UIKit, Views other than ContentView are increasingly required and do not have to be named ContentView.

In detail, you can create a screen design using Swift's code without using storyboard in UIKit, or SwiftUI has a graphical editor, but you don't have to worry too much about it when you understand the outline.

You may not understand some of the words mentioned in the above description, but please look them up little by little.

To sum up,

  • "ContentView is used for reference books" → The reference book is about SwiftUI
  • "All the articles on ViewController appear" → Those articles are about UIKit

You can say thatAs I mentioned above, UIKit has been around for a long time, so if you search normally, it will be overwhelmingly stuck.

  • When searching, put the word "swiftui" in the search
  • Use the tag "swiftui" to ask questions here

You should remember that


2022-09-30 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.