Ui basics (I), UI BASICS (

Source: Internet
Author: User

Ui basics (I), UI BASICS (

This article and later sections mainly explain some of the knowledge and key points that must be mastered in studying the UI and IOS development. Of course, it is best to learn these knowledge based on c and oc. If there is no basis, we recommend that you first read the zero-basic learning IOS development explained by the president of Guangzhou xiaocode Education "jie" and the president of IOS College "Li Nanjiang.

 

The first ios Program

1 @ interface ViewController: UIViewController 2 3 @ property (nonatomic, weak) IBOutlet UILabel * lable; 4 5 @ end 6 7 8 9 @ interface ViewController () 10 11 @ end12 13 @ implementation ViewController14 15/** 16 red button 17 */18-(IBAction) redBtnClick19 {20 //-[ViewController redBtnClick] 21 NSLog (@ "% s", _ func _); 22 // get the UILabel object and modify the attributes of the object, change the label object to Red 23 self. lable. textColor = [UIColor redColor]; 24 self. lable. text = @ "I Am a red text"; 25 self. lable. backgroundColor = [UIColor purpleColor]; 26 self. lable. font = [UIFont systemFontOfSize: 30]; 27 self. lable. textAlignment = NSTextAlignmentCenter; 28} 29/** 30 green button 31 */32-(IBAction) greenBtnClick33 {34 NSLog (@ "% s", _ func __); 35 self. lable. textColor = [UIColor greenColor]; 36} 37/** 38 blue button 39 */40-(IBAction) blueBtnClick41 {42 NSLog (@ "% s ", _ func _); 43 self. lable. textColor = [UIColor blueColor]; 44} 45 @ end

 

I. Controller

1. What is a controller:

Any class that inherits from UIViewController is called a controller.

2. Role of the controller:

Manage the UI (responsible for managing the creation of the UI and handling of some events)

3. Notes:

The UI interface can be connected to the corresponding controller. We can establish a certain relationship between the elements on the UI interface and the code in the Controller through the link; by default, elements in the UI interface cannot be associated with the method. to associate the elements in the UI interface, you must change the return value of the method to IBAction.

 

Ii. IBAction and IBOutlet  
  • IBAction

-(IBAction) redBtnClick;

1. IBAction

1.1 from the perspective of return values, the function is equivalent to void

1.2 connections can be established with controls in storyboard only when the return value is declared as IBAction.

1.3 IBAction can only be added to methods, but not attributes.

 

2. IBAction connections

Connect from "controller" to "Storyboard"

Connect from "Storyboard" to "controller"

Connect directly to the top of the "Storyboard" Interface

Connect to the toolbar on the Storyboard directly in the Storyboard.

You do not need to define the method first, directly connect from "Storyboard" to "controller" (commonly used)

 

3. Notes for connecting IBAction:

3.1 When copying elements in Storyboard

The original connections are copied together.

One method can associate multiple controls

A control can be connected to many methods. It is generally not written in this way during development.

3.2 If you delete the method associated with the button, a classic error will be reported after the operation.

Reason: '-[ViewController redBtnClick]: unrecognized selector sent to instance 0x7fb4aa618e50'

3.3 IBAction can only be used as the return value of a method.

 

 

  • IBOutlet

@ Property (nonatomic, weak) IBOutlet UILabel * lable;

1. IBOutlet

1.1 only attributes declared as IBOutlet can be connected to the control in storyboard

1.2 to connect a property, you must add IBOutlet before the data type.

2. Several IBOutlet Connection Methods

Connect from "controller" to "Storyboard"

Connect from "Storyboard" to "controller"

Connect directly to the top of the "Storyboard" Interface

Connect to the toolbar on the Storyboard directly in the Storyboard.

You do not need to define a method to connect directly from "Storyboard" to "controller ".

3. Notes:

A control can be associated with multiple attributes.

One attribute cannot be associated with multiple controls

During attribute connection, Xcode will automatically detect the type. If the type does not match, the link cannot be connected.

If you connect the property to the control and then delete the property, a classic error will be reported as long as the program is running.

'[<ViewController 0x7fe9d9f1a5d0> setValue: forUndefinedKey:]: this class is not key value coding-compliant for the key lable .'

Any UI control can be connected to properties, but not any controls can be connected to methods. Only controls inherited from UIControl can be connected.

 

3. UIView

+ What are controls?

-All UI elements on the screen are called controls, or views and components.

-Buttons (UIButton) and text (UILabel) are all controls.

 

+ What are the common attributes of controls?

-Dimensions

-Location

-Background Color

-......

 

+ Apple extracts the common attributes of the control to the parent class UIView.

-All controls are ultimately inherited from UIView.

-Both UIButton and UILabel are inherited from UIView (you can view the header file)

 

+ Each controller has a default UIView attribute.

-@ Property (nonatomic, retain) UIView * view;

-All other controls managed in the controller are child controls (directly or indirectly) of the view)

 

+ Common properties and methods of UIView

1 + @ property (nonatomic, readonly) UIView * superview; 2 // get your parent control object 3 4 + @ property (nonatomic, readonly, copy) NSArray * subviews; 5 // obtain all of your child control objects 6 7 + @ property (nonatomic) NSInteger tag; 8 // Control ID (identifier ), the parent control can use tags to find the corresponding child control 9 10 + @ property (nonatomic) CGAffineTransform transform; 11 // control deformation attributes (attributes such as rotation angle, proportional scaling, and translation can be set) 12 13 + @ property (nonatomic) CGRect frame; 14 // position and size of the control rectangle in the parent control (with the coordinate origin in the upper left corner of the parent Control) 15 16 + @ property (nonatomic) CGRect bounds; 17 // The position and size of the rectangle frame of the control (take the upper left corner as the coordinate origin, so the x and y of bounds are generally 0) 18 19 + @ property (nonatomic) CGPoint center; 20 // The point of the control (with the upper left corner of the parent control as the coordinate origin) 21 22 23 24 25-(void) addSubview :( UIView *) view; 26 // Add a child control view27 28-(void) removeFromSuperview; 29 // remove the 30 31-(UIView *) viewWithTag :( NSInteger) tag from the parent control; 32 // find the corresponding control based on a tag ID (generally child Control)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.