Chapter 5 controls and actions of iOS

Source: Internet
Author: User
Tags button attributes

Target

Review basic component windows and views

Understand common view components in IOS UIKit framework

Display Control-label UILabel

Display Control-text box control UITextField

Control-button UIButton

Control-switch UISwitch

Control-slider UISlider

Other controls-segment controls and paging controls

1. Review basic component windows and views

UIWindow

An application has only one window, which is an instance of UIWindow.

The initial border is the size of the entire screen.

Supports window stacked placement

UIView

One window and multiple views

View is responsible for a display area of the screen

View nesting

A view can have multiple subviews

Respond to user touch events

 

UIImageView

View for storing images

 

2. Understand common components in IOS UIKit framework

View Components

Container

Provides additional visual separation for View content

//////

Window

Provide a place for drawing UIWindow

 

Display View

For simple information display

UILabel, UITextView

UIImageView

Warning view and Action Form

Get user attention

UIAlertView

UIActionSheet

Navigation View

Provides users with a navigation tool from one screen to another.

UITabBar

UINavigationBar

Table View

Views used to display data

UITableView

Selector View

Use the scroll wheel to select the data

UIPickerView

Image view

View container for placing images, transparent box

UIImageView

Scroll View

UIScrollView

Text View

UITextView

 

 


 

 

3. Display Control-label UILabel

UILabel

Read-only view, displaying one or more lines of text

(1) Create

CGRect labelFrame = CGRectMake (0, 10,100, 50 );

UILabel * myLabel = [[UILabel alloc] initWithFrame: labelFrame];

 

(2) set the color

MyLabel. backgroundColor = [UIColor clearColor];

MyLabel. textColor = [UIColor redColor];

 

(3) set the font

MyLabel. font = [UIFontfontWithName: @ "Verdana" size: 18.0];

 

(4) set multi-line text

MyLabel. numberOfLines = 2;

MyLabel. text = @ "Hello World \ nSecond line ";

 

(5) Add to view

[Self. view addSubview: myLabel];

 

(6) Release

[MyLabelrelease];

 

 

4. Display Control-text box control UITextField

For short text input, generally a single line;

(1) Create

CGRect textRect = CGRectMake );

UITextField * myTextField = [[UITextField alloc] initWithFrame: textRect];

MyTextField. backgroundColor = [UIColorwhiteColor];

(2) set the font

MyTextField. font = [UIFontsystemFontOfSize: 22.0];

MyTextField. adjustsFontSizeToFitWidth = YES;

MyTextField. minimumFontSize = 2.0;

(3) add protocols and control actions

@ Interface MyUIControlViewController: UIViewController <UITextFieldDelegate>

Bind proxy object

-(Void) viewDidLoad

{

......

MyTextField. delegate = self;

}

(4) cancel the keyboard

-(BOOL) textFieldShouldReturn :( UITextField *) textField

{

[TextFieldresignFirstResponder];

ReturnYES;

}

(5) input length judgment

-(BOOL) textField :( UITextField *) textFieldshouldChangeCharactersInRange

:( Nsange) rangereplacementString :( NSString *) string

{IntMAX_CHARS = 10;

NSMutableString * newText = [NSMutableString stringWithString: textField. text];

[NewTextreplaceCharactersInRange: range withString: string];

Return ([newText length] <= MAX_CHARS );

}

 

 

 

 

5. Control controls-click UIButton

Create button

 

Bytes ------------------------------------------------------------------------------------------------------------------------------

Using IB:

(1) create a View and generate three files: xxxViewController. xib, xxxViewController. h, xxxViewController. m,

(2) If you use a graphical tool, open xxxViewController. xib, drag the control from the "Object Library" to the View, and set the button attributes through the "Property checker.

If you need to process the button event, you need to create an IBAction method to process the button event in the xxx. ViewController. h file, and in IB, drag the link button and IBAction.

-(IBAction) buttonPress :( UIButton *) sender;

 

The buttons are generally processed in the xxxViewController. m file and written in the corresponding IBAction method.

-(IBAction) buttonPress :( UIButton *) sender {

// Create a prompt box,

UIAlertView * alert = [[UIAlertViewalloc]

InitWithTitle: @ "title"

Message: @ "button processing method implemented through xib"

Delegate: self

CancelButtonTitle: @ "OK"

OtherButtonTitles: nil];

// A prompt box is displayed.

[Alert show];

}

Bytes ------------------------------------------------------------------------------------------------------------------------------

(3) If you use pure code, open xxxViewController. h and create a button object as the @ propertity attribute.

@ Property (nonatomic, retain, readonly) UIButton * roundedButtonType;

 

Then, write the code in the xxxViewController. m file:

The code for compiling the create button in the-(void) viewDidLoad method is as follows:

 

// Create a Button using the code

RoundedButtonType = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

 

// Set the button position and size. Set the vertex coordinates for the first two parameters, and set the width and height for the last two parameters.

RoundedButtonType. frame = CGRectMake (50.0, 250.0, 88.0, 48.0 );

 

// Set the button title and status

[RoundedButtonType setTitle: @ "Rounded" forState: UIControlStateNormal];

// Set the background color of the button

RoundedButtonType. backgroundColor = [UIColor clearColor];

 

// Set the operation performed by the user on the button and process the button event (callback function)

[RoundedButtonType addTarget: self

Action: @ selector (action :)

ForControlEvents: UIControlEventTouchUpInside];

 

// Set the button number to distinguish multiple buttons

RoundedButtonType. tag = 2;

 

 

You need to write a method for button event processing separately, usually called xxxAction:

-(Void) action :( id) sender

{

// Create a prompt box,

UIAlertView * alert = [[UIAlertView alloc]

InitWithTitle: @ "title"

Message: @ "button processing method implemented by pure encoding"

Delegate: self

CancelButtonTitle: @ "OK"

OtherButtonTitles: nil];

// A prompt box is displayed.

[Alert show];

}

 

Bytes ------------------------------------------------------------------------------------------------------------------------------

Custom button

(1) create a button with an image

[MyButton setImage: [UIImageimageNamed: @ "myButtonImage.png"]

ForState: UIControlStateNormal];

 

 

6. Control controls-switch UISwitch

Create Switch

Initialization

CGRect switchRect = CGRectMake (, 50 );

UISwitch * mySwitch = [[UISwitch alloc] initWithFrame: switchRect];

 

 

Add event processing

[MySwitch addTarget: self

Action: @ selector (switchAction :)

ForControlEvents: UIControlEventValueChanged];

 

 

Switch

[MySwitch setOn: YES animated: YES];

 

Modify switch appearance

Obtain the two labels of the switch.

UIView * mainView = [[[mySwitch subviews] objectAtIndex: 0] subviews] objectAtIndex: 2];

UILabel * onLabel = [[mainView subviews] objectAtIndex: 0];

UILabel * offLabel = [[mainView subviews] objectAtIndex: 1];

 

 

Modify font and color

OnLabel. text = @ "YES ";

OffLabel. text = @ "NO ";

OnLabel. textColor = [UIColor yellowColor];

OffLabel. textColor = [UIColor greenColor];

 

 

 

7. Control controls-slider UISlider

Create

CGRect sliderRect = CGRectMake (20,50, 280,40 );

UISlider * mySlider = [[UISlider alloc] initWithFrame: sliderRect];

MySlider. minimumValue = 0;

MySlider. maximumValue = 100;

MySlider. continuous = YES;

......

 

 

 

 

Handle slide events

-(Void) sliderAction :( id) sender

{

IntstepAmount = 10;

FloatstepValue = (abs ([(UISlider *) sender value])/stepAmount) * stepAmount;

[SendersetValue: stepValue];

LblSliderValue. text = [NSString stringWithFormat: @ "% d", (int) stepValue];

}

 

 

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.