iphone与android一样都包含一些常见的控件比如按钮,图片控件(ImageView)、TableViewController(android下为listView)、标签(android下为TextView)、进度条等。今天我们就来学习一下这些控件的使用。
新建一个基于View的项目,添加下图中的控件
我们添加了Label、TextField、Slider、SwitchButton、ToggleButton控件。下面我们需要为TextField和Label控件(显示Slider值的控件图中35)设置输出口以及为按钮设置事件监听。不要忘了将控件与输出口及事件建立连接,方法:按住Control从File's owner 拖到控件,这时会弹出相关的输出口或事件。
在ViewController文件中加入一下代码:
<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;
#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)backgroundTap:(id)sender 隐藏输入法,但用户点击程序任意位置时
- (IBAction)sliderChanged:(id)sender; 当Slider值改变时,Label的text设置为Slider的Value。
到此我们对一些常用的小控件有了一定的了解,接下来我们将学习TableView控件,相当于android平台的ListView控件。
好了就写这么多,有什么问题请留言,大家一起学习交流!
说明:转载请注明出处!