A basic understanding

1. Development steps

Technical point Development steps
UI(User Interface) Build the basic software interface
Network request, JSON Obtaining Network Data
The Model, the UITableView Display data to software interface
## 2. Components of the UI interface
Every element on the UI interface is an object Everything is an object
——–
Text object UILabel object
Image objects UIImageView object
The button object UIButton objects
# # 3. The framework
  • Apple provides developers with a very powerful UIKit framework that contains a rich variety of UI elements
  • There are a lot of frameworks you need to learn in order to create a good and complete iOS app, such as QuartzCore, MapKit, CoreLocation, AVFoundation, etc., each of which has specific utility functions
  • IOS development, standing on the shoulders of giants programming

New project

1. Project configuration

Product Name The project name
Organization Name Name of organization/company
Organization Identifier Unique identification of the organization/company: e.g. company domain (com.xxx)
## 2. A quick look at storyboard files
  • Used to describe a software interface
  • Describes the relationships between interfaces
  • The controller is used to manage the software interface and handle interactive events on the software interface
  • Listen for button click events:
/// IBAction decorates the return type of the method. The return value is void - (IBAction)methodName;Copy the code

Whenever the button triggers an event, the associated method in the controller is automatically called

  • UI objects in a storyboard are accessed via properties:
@property (nonatomic, weak) IBOutlet UILabel *label;Copy the code
  • Simple startup principle: When you run the program, you first load the main. storyboard file (Xcode’s default configuration). Create the controller object pointed to by the arrow – > to display the software interface managed by the controller

3. Use of class extensions

  • Attributes are not required. Do not declare them in header files. Declared in the class extension of the.m file
@interface ViewController ()    
@Property (nonatomic, weak) IBOutlet UILabel *label;
@end
Copy the code
  • Class extensions, written in a.m file, but also in a.h (header file) will not give an error
  • The.m file cannot be imported and the class will be redefined

4. Understanding of UIViewController

  • All objects that inherit from UIViewController are controllers
  • Responsible for handling various events on the software interface (such as button click events)
  • Responsible for software interface creation and destruction
  • A controller manages only one software interface

5. Use of simulators

  • Support different iOS systems, support switching models
  • Rotate, Shake, Home, Lock, and screenshots are supported
  • Supports location and mobile phone language setting

Common mistakes

  • The property code has been removed, the property wires are still in place, and the excess wires need to be removed
'[<ViewController 0x7f9669904b90> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key label.'
Copy the code
  • A method that does not exist was called
'[ViewController test:]: unrecognized selector sent to instance 0x7f99a0c063c0'
Copy the code

UIView

1. Brief introduction

  • What is a control? All UI elements on the screen are called controls/Views/components buttons (UIButtons), text (UILabel) and so on
  • What are the common properties of the control? Size, position, background color, etc
  • Apple extracts all the common properties of the controls into the parent class UIView and all the controls end up inheriting from UIView
  • Every control is a container for other controls and the controls inside are children of the controls outside and the controls outside are the parents of the controls inside and every controller (UIViewController) has a default UIView property inside that it calls automatically once its View is loaded
- (void)viewDidLoad {
	[super viewDidLoad];
}
Copy the code

2. Common attributes

  • Gets your own parent control object
@property(nullable, nonatomic,readonly) UIView *superview;
Copy the code
  • Gets all of its child control objects
@property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *subviews;
Copy the code
  • The ID of the parent control can be used to find the corresponding child control through the tag
@property(nonatomic) NSInteger tag; // default is 0
Copy the code
  • Control’s deformation properties (you can set rotation Angle, scale, pan, etc.)
@property(nonatomic) CGAffineTransform transform;
Copy the code

3. Common methods

  • Add a child control View
- (void)addSubview:(UIView *)view;
Copy the code
  • Removes from the parent control
- (void)removeFromSuperview;
Copy the code
  • Find the corresponding control view traversal order according to a tag: first check whether the current control’s tag matches the first child control’s tag, check whether the second child control matches…… This method is not recommended because it has performance problems and is difficult to maintain
- (nullable __kindof UIView *)viewWithTag:(NSInteger)tag;
Copy the code

4. Position and size

  • Frame, bounds, Center
// The position and size of the rectangle in the parent control (starting from the upper left corner of the parent control) @property(nonatomic) CGRect frame; // The position and size of the rectangle (starting from its own upper left corner) @property(nonatomic) CGRect bounds; @property(nonatomic) CGPoint Center; // The position of the center point of the control (starting from the upper left corner of the parent control) @property(nonatomic) CGPoint Center;Copy the code

UIImageView

1. frame

  • initWithImage
// Create an iconImageView from the incoming image, And the size of iconImageView is equal to the size of the image UIImageView *iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];Copy the code
  • Error code:
/ / error: cannot be directly modified OC imageView object structure properties of members. The frame. The size, width = 30;Copy the code
  • The most common way to write this is to use the CGRectMake function to use the temporary structure variable and assign directly to the structure
CGRect tempFrame = imageView.frame; tempFrame.size.width = 100; imageView.frame = tempFrame; Imageview. frame = (CGRect){{0,0}, {100,100}};Copy the code

2. Resource reference problems

choose instructions
Copy items if needed Unchecked, the project folder is not there, only reference

If possible, it will not affect the source file
Create groups Create a virtual folder, yellow
Create folder references Create a real folder, blue
Add to targets Package the images into the software installation package
// Get sandbox path Po NSHomeDirectory()Copy the code

3. Use of frame animation

@property (nullable, nonatomic, copy) NSArray<UIImage *> *animationImages; @property (nonatomic) NSTimeInterval animationDuration; @property (nonatomic) NSInteger animationRepeatCount; // start animation - (void)startAnimating; /// Stop animation - (void)stopAnimating;Copy the code

4. Obtain the resource path and play the audio file

/ * * resources path: http://www.baidu.com/123/123.png; network resources Local resource file:///Users/ local resource path; NSBundle = [NSBundle mainBundle]; // Return NSURL [bundle URLForResource:@"aaa" withExtension:@"mp3"]; [bundle URLForResource:@"aaa.mp3" withExtension:@"nil"]; // Return NSString [bundle pathForResource:@"aaa" ofType:@"mp3"]; NSURL *url = [NSURL URLWithString:@""]; Self. player = [AVPlayer playerWithURL:url]; [self.player play];Copy the code

5. Load the image without cache

/** Can only be accessed by the name of the image stored in assets. xcassets. [UIImage imageNamed:@"test"]; /** Not in the assets. xcassets folder, through the full path to access as long as the name of the method with file, all full path without cache use scenario: */ NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@" PNG "]; [UIImage imageWithContentsOfFile:imagePath]Copy the code

6. Put things off

[objc performSelector:@selector(test:) withObject:@"123" afterDelay:10]; Call objc's test: method after 10 seconds, passing @"123" // objc can be any objectCopy the code