Usage of common controls for iphone Development

Source: Internet
Author: User

Like android, iphone contains some common controls such as buttons, image views, TableViewController (listView in android), labels (TextView in android), and progress bars. Today, let's take a look at the use of these controls.

Create a View-based project and add controls

The Label, TextField, Slider, SwitchButton, and ToggleButton controls are added. Next we need to set the output port and event listening for the TextField and Label controls (35 in the control chart showing the Slider value) and the button. Do not forget to connect the Control to the output port and event. Method: Press and hold Control to drag the Control from File's owner to the Control. Related output ports or events will pop up.

Add the following code to the ViewController file:

<UIActionSheetDelegate>{    }@property (nonatomic, retain) IBOutlet UITextField *nameField;@property (nonatomic, retain) IBOutlet UITextField *passField;@property (nonatomic, retain) IBOutlet UILabel *sliderLabel;@property (nonatomic, retain) IBOutlet UISwitch *leftSwitch;@property (nonatomic, retain) IBOutlet UISwitch *rightSwitch;@property (nonatomic, retain) IBOutlet UIButton *doSomethingBtn;- (IBAction)toggleControls:(id)sender;- (IBAction)switchChanged:(id)sender;- (IBAction)buttonPressed:(id)sender;- (IBAction)textFieldDoneEditing:(id)sender;- (IBAction)backgroundTap:(id)sender;- (IBAction)sliderChanged:(id)sender;

Modify the ViewController. m file as follows:

#import "ViewController.h"@implementation ViewController@synthesize nameField;@synthesize passField;@synthesize sliderLabel;@synthesize leftSwitch;@synthesize rightSwitch;@synthesize doSomethingBtn;- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Release any cached data, images, etc that aren't in use.}- (IBAction)toggleControls:(id)sender{    if([sender selectedSegmentIndex] == kSwichesSegmentIndex){        leftSwitch.hidden = NO;        rightSwitch.hidden = NO;        doSomethingBtn.hidden = YES;    }else{        leftSwitch.hidden = YES;        rightSwitch.hidden = YES;        doSomethingBtn.hidden = NO;    }}- (IBAction)switchChanged:(id)sender{    UISwitch *whichSwitch = (UISwitch *)sender;    BOOL setting = whichSwitch.isOn;    [leftSwitch setOn:setting animated:YES];    [rightSwitch setOn:setting animated:YES];}- (IBAction)buttonPressed:(id)sender{    UIActionSheet *actionSheet = [[UIActionSheet alloc]                                   initWithTitle:@"Are you sure?"                                   delegate:self                                  cancelButtonTitle:@"No way!"                                  destructiveButtonTitle:@"Yes,I'm sure!"                                  otherButtonTitles:nil, nil];    [actionSheet showInView:self.view];    [actionSheet release];}- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{    if (buttonIndex != [actionSheet cancelButtonIndex]) {        NSString *msg = nil;        if (nameField.text.length > 0) {            msg = [[NSString alloc] initWithFormat:@"You can breathe easy, %@, everything went OK.", nameField.text];        }else{            msg = @"You can breathe easy, everything went Ok.";        }        UIAlertView *alert = [[UIAlertView alloc]                               initWithTitle:@"Something was done"                               message:msg                               delegate:self                               cancelButtonTitle:@"Phew!"                               otherButtonTitles:nil, nil];        [alert show];        [alert release];        [msg release];    }    }- (IBAction)textFieldDoneEditing:(id)sender{    [sender resignFirstResponder];  //Hidden keyboard}- (IBAction)backgroundTap:(id)sender{    [nameField resignFirstResponder];    [passField resignFirstResponder];}- (IBAction)sliderChanged:(id)sender{    UISlider *slider = (UISlider *)sender;    int progress = (int)slider.value;    NSString *labelText = [[NSString alloc] initWithFormat:@"%d",progress];    sliderLabel.text = labelText;    [labelText release];}#pragma mark - View lifecycle- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)viewDidUnload{    self.nameField = nil;    self.passField = nil;    self.sliderLabel = nil;    self.leftSwitch = nil;    self.rightSwitch = nil;    self.doSomethingBtn = nil;    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];}- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];}- (void)viewWillDisappear:(BOOL)animated{[super viewWillDisappear:animated];}- (void)viewDidDisappear:(BOOL)animated{[super viewDidDisappear:animated];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // Return YES for supported orientations    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);    } else {        return YES;    }}- (void) dealloc{    [nameField release];    [passField release];    [sliderLabel release];    [leftSwitch release];    [rightSwitch release];    [doSomethingBtn release];    [super dealloc];}@end

-(IBAction) toggleControls :( id) sender when the ToggleButton button is pressed, the other button is set to the same status.
-(IBAction) switchChanged :( id) sender handles the Switch button event and hides the toggle button.
-(IBAction) buttonPressed :( id) sender responds to the button event when toggle hides the Button display.
-(IBAction) textFieldDoneEditing :( id) sender; hides the Input Panel. When the user presses the Done button of the input method

-(IBAction) backgroundTap :( id) when the sender hides the input method, but the user clicks any position of the program
-(IBAction) sliderChanged :( id) sender; when the Slider Value changes, the text of the Label is set to the Value of the Slider.

At this point, we have some knowledge about some commonly used Widgets. Next we will learn the TableView controls, which are equivalent to the ListView controls on the android platform.

Now, let's write so much. If you have any questions, please leave a message so that you can learn and communicate with each other!

Note: Please indicate the source for reprinting!

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.