introduce

IOS is a mobile operating system developed by Apple. Apple first announced the system (originally called iPhone Runs OS X) at Macworld on January 9, 2007. The system was originally designed for the iPhone (hence the name iPhone OS) and has since been rolled out to devices such as the iPod Touch, iPad, and Apple TV (hence the change to iOS announced at WWDC 2010).

As a developer, it’s especially important to have a learning atmosphere and a networking community, and this is one of my iOS networking groups:812157648, no matter you are small white or big ox welcome to enter, share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning growth together!

IOS History

A new version will be released each year from 2007 to 2020, with the latest version iOS 14(this tutorial is based on iOS 14).

Apple official website and developer website

  • www.apple.com

  • developer.apple.com

The development of hardware

  • Apple computers: iMac or MacBook or Mac Mini hosts are connected to a monitor.

  • You can also install a black Apple operating system on a regular computer (not recommended), which is complicated and unstable to install.

Develop software

IOS uses Xcode tools for development. You can search for it in the App Store or download it from the Apple Developer website (this tutorial is based on Xcode 12).

Development of language

Swift or Objective-C(this tutorial is based on Swift 5.x).

Developer account

  • $99 per person/year

  • Company $99 / year

  • $199 / year for enterprises

There are four levels of iOS

  • Core OS is the most Core system layer, including memory management, file system, hardware management, power management, security management and so on.

  • Core Services includes a variety of Core Services for App use, such as network, threading, location, etc.

  • The Media layer mainly includes the processing of various Media files. Through it, we can use various Media files in the application, record audio and video, draw graphics, and make basic animation effects.

  • The Cocoa Touch layer provides a variety of useful frameworks for application development, mostly related to the user interface, which is responsible for user Touch interaction on iOS devices, as well as some other key functions.

Create your first iOS project

The project file

The App Settings

  • The application of

    • Project – > Target – > General – > Display Name
  • Application icon

  • Startup screen LaunchScreen

    • The LaunchScreen affects resolution and can be removed for verification by LaunchScreen.

The simulator

App initialization process

AppDelegate

  • @main(iOS 14 used to be @UIApplicationMain)

  • DidFinishLaunchingWithOptions boot method

  • Entrusted to SceneDelegate

SceneDelegate

  • window

  • The life cycle

class SceneDelegate: UIResponder, UIWindowSceneDelegate {var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {guard let _ = (scene as? UIWindowScene) else { return }}func sceneDidDisconnect(_ scene: UIScene) {}func sceneDidBecomeActive(_ scene: UIScene) {}func sceneWillResignActive(_ scene: UIScene) {}func sceneWillEnterForeground(_ scene: UIScene) {}func sceneDidEnterBackground(_ scene: UIScene) {}}Copy the code

Initialize the

Main Storyboard

  1. Parse info.plist to get the Application Scene Manifest and find the Storyboard set to the Main Storyboard file base name.

  2. @main

  3. AppDelegate – > SceneDelegate

  4. Displays the controllers initialized in main.storyboard.

Storyboard is introduced
  • Container concept

  • Is Initial View Controller

  • The connection between interfaces in Storyboard and ViewController.swift (an interface is associated with a class file)

Pure code

  1. Delete the Main Storyboard file base name from info.plist and the innermost Storyboard name from the Application Scene Manifest.

  2. Parsing info.plist shows that the Main Storyboard is not set.

  3. @ main.

  4. AppDelegate – > SceneDelegate

  5. Pure code initializes UIWindow in SceneDelegate’s willConnectToSession and sets the first controller to display. Window — > rootViewController — > UIViewController — > UIView.

UIView and UIViewController

First impression

In iOS development, an interface is a UIViewController, and the content displayed on the interface is a UIView.

UIView versus UIViewController

UIViewController by default has a UIView that’s the size of the screen, and UIViewController manages its life cycle. All UI controls placed on the interface are placed on UIView of UIViewController, which is available in UIViewController code via the self.view property. All other UIViews (and their subclasses) under development are placed on this view.

UIView lifecycle function of UIView in UIViewController

  • ViewDidLoad: View completes memory loading

  • ViewWillAppear: View is about to display

  • ViewDidAppear: The View is fully displayed

  • ViewWillDisappear: View is about to disappear

  • ViewDidDisappear: View disappears completely

UIView

IOS coordinate system

  • Two dimensional coordinate system

UIView the View added to UIViewController

The container concept of UIView

  • You can put other UIViews in UIView

  • UIView hierarchy (flat vs. inclusive)

UIView common properties

  • BackgroudColor: background color

  • Frame: Coordinates and size relative to the superview

  • Bounds: Coordinates and sizes relative to themselves, so bounds x and y are always 0.

  • Center: coordinates of the central point relative to the superview

  • Alpha: Transparency (0.0 ~ 1.0)

  • Tag: The tag (Int, 0 by default) that can be retrieved from the container’s view. viewWithTag method.

  • Title: the title

  • Superview: superview

  • Subviews: Indicates all subviews

Common methods of UIView

  • AddSubview: Adds a view to the superview

  • RemoveFromSuperview: Remove the view from the parent view.

  • ViewWithTag: Gets a view based on the tag value

  • InsertSubview: Inserts a view above/below the specified view

  • BringSubviewToFront: Move the view to the top layer

  • ExchangeSubviewAtIndex: Swaps views at 2 locations

Demo UIView and UIViewController

  • Storyboard

    • Notice the view hierarchy on the left panel

    • An introduction to important properties in the properties panel

  • Pure code

    • Corresponds to the properties in the Storyboard properties panel

Pay attention to where the code is written. Some people often write the code incorrectly.

@ IBOutlet and @ IBAction

The introduction of

How do I get a custom UIView in my Storyboard in code? There are currently two ways:

  • Through the Tag attribute: If there are too many views, or you forget to set the tag, or you set the same tag, there will be problems.

  • By subviews: It is difficult to accurately obtain a View through the array once there are many views.

So is there something friendlier, more intuitive, more convenient, more efficient? And the answer is yes, that’s @ibOutlet and @ibAction.

@IBOutlet

  • Properties written to a class.

  • Make a connection to UIView in your Storyboard.

  • And then the property represents the UIView in the Storyboard

  • All UI controls can drag and drop @IBOutlet.

@IBAction

  • A method written to a class.

  • Relate to events generated by UIView in Storyboard.

  • The method is called when the corresponding event occurs.

  • Only UI controls that inherit from UIControl can drag @ibAction.

Forward and reverse drag cables

  • Drag and drop wires: Drag and drop wires from a Storyboard to a class file.

  • Reverse drag and drop: Drag and drop wires from the class file to the Storyboard.

The premise of connection

The UIViewController in your Storyboard is associated with the class you want to drag.

Classic wiring error

  • UIView has been wired up and removed the @ibOutlet or @ibAction from the class.

  • UIView is wired to change the @ibOutlet or @ibAction name in that class.

  • The error message

  1. @ibAction: Unrecognized selector sent to instance

  2. IBOutlet: This class is not key value coding-compliant for the key XXX

Original text author: coloured glaze yarn

Original address: 0x9.me/Z7dh6