IOS --- methods for creating and calling various controls

Source: Internet
Author: User

1. Image View

// Find the image
UIImage * pImage = [UIImage imageNamed: @ "sea.png"];
// Create an ImageView Based on the Content Image
Self. pImageView = [[UIImageView alloc] initWithImage: pImage];
// Place the ImageView to the parent View
[Self. view insertSubview: self. pImageView atIndex: 0];
2. Label
// Create a label
UILabel * pLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 10,120, 50)];
// Add content
PLabel. text = @ "HelloWorld \ nSecondLine ";
// Set the font and size
PLabel. font = [UIFont fontWithName: @ "Verdana" size: 18];
// Font alignment
PLabel. textAlignment = NSTextAlignmentCenter;
// Font color
PLabel. textColor = [UIColor redColor];
// Display the number of rows
PLabel. numberOfLines = 2;
// Shadow color
PLabel. shadowColor = [UIColor blackColor];
// Shadow size
PLabel. shadowOffset = CGSizeMake (2.0, 1.0 );
// Set the background color of the label to transparent.
PLabel. backgroundColor = [UIColor clearColor];
[Self. view addSubview: pLabel];
[PLabel release];
3. TextField
// Create TextField
UITextField * pTextField = [[UITextField alloc] initWithFrame: CGRectMake (10,116,200, 31)];
// Set the border Style
PTextField. borderStyle = UITextBorderStyleRoundedRect;
// Set the font
PTextField. font = [UIFont systemFontOfSize: 18.0];
// Change the font according to the width
PTextField. adjustsFontSizeToFitWidth = YES;
// The smallest font
PTextField. minimumFontSize = 2.0;
// Clear the button style
PTextField. clearButtonMode = UITextFieldViewModeWhileEditing;
// Pop-up keyboard Style
PTextField. keyboardType = UIKeyboardTypeDefault;
// Set the automatic correction function
PTextField. autocorrectionType = UITextAutocorrectionTypeNo;
// Set the keyboard auto-case attributes
PTextField. autocapitalizationType = UITextAutocapitalizationTypeNone;
// Set the return button type
PTextField. returnKeyType = UIReturnKeyDone;
// Set whether Password text display is supported
PTextField. secureTextEntry = YES;
// Set Delegation
PTextField. delegate = self;
[Self. view addSubview: pTextField];
[PTextField release];
The Protocol UITextFieldDelegate is required for writing the method as follows:
// Method for returning the keyboard
-(BOOL) textFieldShouldReturn :( UITextField *) textField
{
// Put the current textField to discard the first responder
[TextField resignFirstResponder];
Return YES;
}
// Touch the background and play back the keyboard as follows:
-(IBAction) backgroundTap :( id) sender {
[_ NumberField resignFirstResponder];
[_ CodeField resignFirstResponder];
}
// A Simple keyboard bounce method (touch the background)
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
{
[Self. textField endEditing: YES];
}
// Restrict the length of the input string
-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string
{
Int MAX_LENGTH = 10;
NSMutableString * pNewString = [NSMutableString stringWithString: textField. text];
// Complete replacement of strings in the range
[PNewString replaceCharactersInRange: range withString: string];
// Returns YES or NO based on the two lengths.
Return ([pNewString length] <= MAX_LENGTH );
}
4. Button
// Create a button
UIButton * pBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
// Set the region
[PBtn setFrame: CGRectMake (10, 70,100, 40)];
// Set the name
[PBtn setTitle: @ "Normal" forState: UIControlStateNormal];
[PBtn setTitle: @ "HighLight" forState: UIControlStateHighlighted];
// Highlight allowed
PBtn. showsTouchWhenHighlighted = YES;
// Button call Method
[PBtn addTarget: self action: @ selector (buttonDown :) forControlEvents: UIControlEventTouchDown];
[Self. view addSubview: pBtn];
// Response method of the button
-(Void) buttonDown :( id) sender
{}
5. CheckboxButton (selected box)
// Create a custom button
UIButton * pCheckboxButton = [UIButton buttonWithType: UIButtonTypeCustom];
// Set the region
CGRect checkboxRect = CGRectMake (10,155 );
[PCheckboxButton setFrame: checkboxRect];
// Set the corresponding image for two different States respectively
[PCheckboxButton setImage: [UIImage imageNamed: @ "checkbox_off.png"] forState: UIControlStateNormal];
[PCheckboxButton setImage: [UIImage imageNamed: @ "checkbox_on.png"] forState: UIControlStateSelected];
// Add association Action
[PCheckboxButton addTarget: self action: @ selector (checkboxClick :) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: pCheckboxButton];
// Correlation Method
-(Void) checkboxClick :( UIButton *) btn
{
Btn. selected =! Btn. selected;
}
6. Slideer -- Slider
// Create a UISlider object
UISlider * pSlider = [[UISlider alloc] initWithFrame: CGRectMake (10,195,300, 20)];
// Set the minimum value
PSlider. minimumValue = 0;
// Maximum value
PSlider. maximumValue = 100;
// Whether sliding can continue
PSlider. continuous = YES;
// Set the Initial Value
PSlider. value = 50;
// Transparency
Self. pImageView. alpha = pSlider. value/100;
// The image at the minimum value
PSlider. minimumValueImage = [UIImage imageNamed: @ "apple_min"];
// The image at the maximum value
PSlider. maximumValueImage = [UIImage imageNamed: @ "apple_max.png"];
// Set the current image
[PSlider setThumbImage: [UIImage imageNamed: @ "apple_thumb.png"] forState: UIControlStateNormal];
// Add the corresponding action. Note: CotrolEvents: valueChanged
[PSlider addTarget: self action: @ selector (sliderValueChange :) forControlEvents: UIControlEventValueChanged];
[Self. view addSubview: pSlider];
// Create a label that displays the current value
Self. pShowValueLabel = [[UILabel alloc] initWithFrame: CGRectMake (180,150, 86, 25)];
// Set the background color
Self. pShowValueLabel. backgroundColor = [UIColor lightGrayColor];
// Set the current text content
Self. pShowValueLabel. text = [NSString stringWithFormat: @ "brightness: % d", (int) pSlider. value];
[Self. view addSubview: self. pShowValueLabel];
// Correlation Method
-(Void) sliderValueChange :( UISlider *) slider
{
// Display the current brightness
Self. pShowValueLabel. text = [NSString stringWithFormat: @ "brightness: % d", (int) slider. value];
// Transparency
Self. pImageView. alpha = slider. value/100;
}
7. Switch -- Switch
UISwitch * pSwitch = [[UISwitch alloc] initWithFrame: CGRectMake (220, 10, 10, 50)];
// Add associated actions to the switch
[PSwitch addTarget: self action: @ selector (switchMethod :) forControlEvents: UIControlEventValueChanged];
[Self. view addSubview: pSwitch];
// Correlation Method
-(Void) switchMethod :( id) sender
{
// Find the switch
UISwitch * pSwitch = (UISwitch *) sender;
If (pSwitch. isOn) // set the background image hidden Based on the switch status
{
[Self. pImageView setHidden: NO];
}
Else
{
[Self. pImageView setHidden: YES];
}
}
8. SegmentControl-segment Control
// Create an array
NSArray * pArr = [NSArray arrayWithObjects: @ "1", @ "2", nil];
// Initialize the SegmentControl object based on the array object
UISegmentedControl * Specified gment = [[UISegmentedControl alloc] initWithItems: pArr];
[Sequence gment setFrame: CGRectMake (10,240,300, 40)];
// Set the Style of the segmented Control
[Sequence gment setSegmentedControlStyle: UISegmentedControlStyleBordered];
// Set the number of segments selected by the segment Control
[Export gment setSelectedSegmentIndex: 0];
// Add association Action
[Export gment addTarget: self action: @ selector (segmentMethod :) forControlEvents: UIControlEventValueChanged];
[Self. view addSubview: Variable gment];
9. Progress -- Progress bar
// Create a progress bar and initialize it
Self. pProgress = [[UIProgressView alloc] initWithFrame: CGRectMake (10,360,300, 30)];
// Set the Style
Self. pProgress. progressViewStyle = UIProgressViewStyleBar;
// Set the unfinished color for the progress bar.
Self. pProgress. trackTintColor = [UIColor redColor];
Self. pProgress. progressTintColor = [UIColor greenColor];
[Self. view addSubview: self. pProgress];
// Create a label to display the progress
Self. pProgressValueLabel = [[UILabel alloc] initWithFrame: CGRectMake (100,370,110, 30)];
Self. pProgressValueLabel. backgroundColor = [UIColor clearColor];
Self. pProgressValueLabel. text = [NSString stringWithFormat: @ "load: %. 1f", self. progressValue * 100];
[Self. view addSubview: self. pProgressValueLabel];
// Use the timer to call the task cyclically.
PTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (progressMethod :) userInfo: nil repeats: YES];
// Associated Method
-(Void) progressMethod :( id) sender
{
Self. progressValue ++ = 0.1;
Self. pProgress. progress = self. progressValue;
Self. pProgressValueLabel. text = [NSString stringWithFormat: @ "load: %. 1f", self. progressValue * 100];

// [Self. pActivity startAnimating];
If (self. progressValue> = 1)
{
Self. progressValue = 0;
Self. pProgressValueLabel. text = @ "loaded ";
// [Self. pActivity stopAnimating];
// Self. pActivity. hidden = NO;
// Stop the timer
[PTimer invalidate];
}
}
10. ActivityIndicatorView
// Create and initialize ActivityIndicatorView (Style)
Self. pActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
[Self. pActivity setFrame: CGRectMake (10,280, 45, 45)];
// Set whether to hide
Self. pActivity. hidden = NO;
[Self. view addSubview: self. pActivity];
// Create button to associate the corresponding method
UIButton * pBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[PBtn setFrame: CGRectMake (10,330,)];
[PBtn setTitle: @ "" forState: UIControlStateNormal];
[PBtn addTarget: self action: @ selector (doRotate :) forControlEvents: UIControlEventTouchUpInside];
PBtn. tag= 110;
[Self. view addSubview: pBtn];
// Associated Method
-(Void) doRotate :( id) sender
{
UIButton * pBtn = (UIButton *) sender;
[Self. pActivity isAnimating]? [Self. pActivity stopAnimating]: [self. pActivity startAnimating];
// Set the button title based on whether the Activity is running
[PBtn setTitle :( self. pActivity. isAnimating? @ "Stop": @ "") forState: UIControlStateNormal];
Self. pActivity. hidden = NO;
}


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.