iPhone開發(fā)常用控件:UIActionSheet和UIAlertView學(xué)習(xí)
iPhone開發(fā)常用控件UIActionSheet和UIAlertView的學(xué)習(xí)是本文要介紹的內(nèi)容,主要來學(xué)習(xí)iphone開發(fā)中的控件如何來使用,來看本文詳細(xì)內(nèi)容。
一、UILabel
二、UIButton
常用事件:Touch Up Inside
三、UITextField
常用屬性:
Text:要顯示的文本。
Placeholder:指定將要在文本字段中以灰色顯示的占位符文本。
Clear When Editing Begins:用戶觸摸此字段時(shí)是否刪除字段中的值。
Text Input Traits:文本輸入特征。
四、UIImageView
常用屬性:
image:指定圖像文件
Mode:圖像在視圖內(nèi)部的對(duì)齊方式以及是否縮放圖像以適應(yīng)視圖。選擇任何圖像縮放的選項(xiàng)都會(huì)潛在地增加處理開銷,因此***避開這些選項(xiàng),并在導(dǎo)入圖像之前調(diào)整好圖像大小。通常Mode屬性為Center。
Alpha:圖像透明度。一般設(shè)置為1.0
Background:該屬性繼承自UIView,但它不會(huì)影響圖像視圖的外觀,請(qǐng)忽略此屬性。
Drawing復(fù)選框:選中Opaque表示視圖后面的任何內(nèi)容都不應(yīng)該繪制,并且允許iPhone都繪圖方法通過一些優(yōu)化來加速繪圖。
Clear Context Before Drawing:選中它之后,iPhone將使用透明黑色繪制控件覆蓋都所有區(qū)域,然后才實(shí)際繪制控件。考慮到性能問題,并且適用情況很少,通常很少需要選中ClearContext Before Drawing。
Interaction復(fù)選框:
User Interaction Enabled:指定用戶能否對(duì)此對(duì)象進(jìn)行操作。
Multiple Touch:是否能夠接收多點(diǎn)觸摸事件。
五、UISlider(滑塊)
常用屬性:Value Changed
示例:
- // 將silder的值反映到sliderLabel
- - (IBAction) sliderValueChanged: (id)sender
- {
- UISlider *slider = (UISlider *)sender;
- int progressAsInt = (int)(slider.value + 0.5f);
- NSString *newText = [[NSString alloc] initWithFormat:@"%d", progressAsInt];
- sliderLabel.text = newText;
- [newText release];
- }
六、UISwitch(開關(guān))
代碼
- // 屬性on:獲取開關(guān)的狀態(tài)是否為on// 方法setOn:設(shè)置開關(guān)的狀態(tài)
- - (IBAction) switchChanged: (id)sender{
- UISwitch *whichSwitch = (UISwitch *)sender;
- BOOL setting = whichSwitch.on;
- [leftSwitch setOn:setting animated:YES];
- [rightSwitch setOn:setting animated:YES];
- }
七、UISegmentedControl
- #define kSegmentIndex_Switches 0
- #define kSegmentIndex_Button 1
- - (IBAction) segmentChanged: (id)sender{
- switch ([sender selectedSegmentIndex]) {
- case kSegmentIndex_Switches:
- leftSwitch.hidden = NO;
- rightSwitch.hidden = NO;
- doSomethingButton.hidden = YES;
- break;
- case kSegmentIndex_Button: leftSwitch.hidden = YES;
- rightSwitch.hidden = YES;
- doSomethingButton.hidden = NO; break;
- }
- }
八、UIActionSheet(操作表)和UIAlertView(警報(bào))
UIActionSheet用于迫使用戶在兩個(gè)或更多選項(xiàng)之間進(jìn)行選擇都模式視圖。操作表從屏幕底部彈出,顯示一系列按鈕供用戶選擇,用戶只有單擊了一個(gè)按鈕后才能繼續(xù)使用使用應(yīng)用程序。
UIAlertView(警報(bào))以藍(lán)色圓角矩形都形式出現(xiàn)在屏幕的中部,警報(bào)可顯示一個(gè)或多個(gè)按鈕。
為了讓控制器類充當(dāng)操作表的委托,控制器類需要遵從UIActionSheetDelegate協(xié)議。我們通過在類聲明都超類之后都尖括號(hào)中添加協(xié)議名稱來實(shí)現(xiàn)。
- @interface UntitledViewController : UIViewController
- <UIActionSheetDelegate>{ // ....}// 創(chuàng)建操作表:
- - (IBAction) buttonPressed: (id)sender{
- UIActionSheet *actionSheet = [[UIActionSheet alloc]
- initWithTitle:@"Are you sure?" delegate:self
- cancelButtonTitle:@"Cancel"
- destructiveButtonTitle:@"Yes,I'm sure."
- otherButtonTitles:nil];
- [actionSheet showInView:self.view];
- [actionSheet release];}// 實(shí)現(xiàn)方法:#pragma mark ActionSheet Delegate Methods
- - (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
- if (buttonIndex != [actionSheet cancelButtonIndex]) {
- NSString *text = [[NSString alloc] initWithFormat:@"test alert"];
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"Something was done."
- message:text delegate:self cancelButtonTitle:@"OK!" ,otherButtonTitles:nil];
- [alert show];
- [alert release];
- [text release];
- }
- }//- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex//{// NSLog(@"%d",buttonIndex);//}
示例
視圖有一個(gè)UISegmentedControl,"Switches"下有兩個(gè)UISwitch
"Button"下有一個(gè)“Do Something"的UIButton
觸摸"Do Something"Button時(shí)彈出UIActionSheet
觸摸選擇"Yes,I'm sure."時(shí)彈出 UIAlertView
- OBJECTIVE-C CODE :UntitledViewController.h
- //
- // UntitledViewController.h
- // Untitled
- //
- // Created by Elf Sundae on 11/10/10.
- // Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #define kSegmentIndex_Switches 0
- #define kSegmentIndex_Button 1
- @interface UntitledViewController : UIViewController
- <UIActionSheetDelegate>
- {
- UISwitch * leftSwitch;
- UISwitch * rightSwitch;
- UIButton * doSomethingButton;
- }
- @property (retain, nonatomic) IBOutlet UISwitch *leftSwitch;
- @property (retain, nonatomic) IBOutlet UISwitch *rightSwitch;
- @property (retain, nonatomic) IBOutlet UIButton *doSomethingButton;
- - (IBAction) switchChanged: (id)sender;
- - (IBAction) segmentChanged: (id)sender;
- - (IBAction) buttonPressed: (id)sender;
- @end
- //
- // UntitledViewController.h
- // Untitled
- //
- // Created by Elf Sundae on 11/10/10.
- // Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #define kSegmentIndex_Switches 0
- #define kSegmentIndex_Button 1
- @interface UntitledViewController : UIViewController
- <UIActionSheetDelegate>
- {
- UISwitch * leftSwitch;
- UISwitch * rightSwitch;
- UIButton * doSomethingButton;
- }
- @property (retain, nonatomic) IBOutlet UISwitch *leftSwitch;
- @property (retain, nonatomic) IBOutlet UISwitch *rightSwitch;
- @property (retain, nonatomic) IBOutlet UIButton *doSomethingButton;
- - (IBAction) switchChanged: (id)sender;
- - (IBAction) segmentChanged: (id)sender;
- - (IBAction) buttonPressed: (id)sender;
- @end
- OBJECTIVE-C CODE :UntitledViewController.m
- //
- // UntitledViewController.m
- // Untitled
- //
- // Created by Elf Sundae on 11/10/10.
- // Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.
- //
- #import "UntitledViewController.h"
- @implementation UntitledViewController
- @synthesize leftSwitch;@synthesize rightSwitch;@synthesize doSomethingButton;// 屬性on:獲取開關(guān)的狀態(tài)是否為on
- // 方法setOn:設(shè)置開關(guān)的狀態(tài)
- - (IBAction) switchChanged: (id)sender{ UISwitch *whichSwitch = (UISwitch *)sender;
- BOOL setting = whichSwitch.on;
- [leftSwitch setOn:setting animated:YES];
- [rightSwitch setOn:setting animated:YES];
- }
- - (IBAction) segmentChanged: (id)sender{
- switch ([sender selectedSegmentIndex]) {
- case kSegmentIndex_Switches:
- leftSwitch.hidden = NO;
- rightSwitch.hidden = NO;
- doSomethingButton.hidden = YES;
- break;
- case kSegmentIndex_Button:
- leftSwitch.hidden = YES;
- rightSwitch.hidden = YES;
- doSomethingButton.hidden = NO;
- break;
- }
- }
- - (IBAction) buttonPressed: (id)sender{ UIActionSheet *actionSheet = [[UIActionSheet alloc]
- initWithTitle:@"Are you sure?"
- delegate:self cancelButtonTitle:@"Cancel"
- destructiveButtonTitle:@"Yes,I'm sure." otherButtonTitles:nil];
- [actionSheet showInView:self.view];
- [actionSheet release];
- }
- - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload { // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- self.leftSwitch = nil;
- self.rightSwitch = nil;
- self.doSomethingButton = nil;
- [super viewDidUnload];
- }
- - (void)dealloc {
- [leftSwitch release];
- [rightSwitch release];
- [doSomethingButton release];
- [super dealloc];
- }
- #pragma mark
- #pragma mark ActionSheet Delegate Methods
- - (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
- if (buttonIndex != [actionSheet cancelButtonIndex]) {
- NSString *text = [[NSString alloc] initWithFormat:@"test alert"];
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"Something was done." message:text
- delegate:self
- cancelButtonTitle:@"OK!" otherButtonTitles:nil];
- [alert show];
- [alert release];
- [text release];
- }
- }//- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
- //{
- // NSLog(@"%d",buttonIndex);
- //}
- @end
小結(jié):iPhone開發(fā)常用控件:UIActionSheet和UIAlertView學(xué)習(xí)的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對(duì)你有所幫助!