iPhone开发常用控件:UIActionSheet和UIAlertView学习

移动开发 iOS
iPhone开发常用控件UIActionSheet和UIAlertView的学习是本文要介绍的内容,主要来学习iphone开发中的控件如何来使用,来看本文详细内容。

iPhone开发常用控件UIActionSheet和UIAlertView的学习是本文要介绍的内容,主要来学习iphone开发中的控件如何来使用,来看本文详细内容。

一、UILabel

iPhone开发常用控件

二、UIButton

iPhone开发常用控件

常用事件:Touch Up Inside

三、UITextField

iPhone开发常用控件

常用属性:

Text:要显示的文本。

Placeholder:指定将要在文本字段中以灰色显示的占位符文本。

Clear When Editing Begins:用户触摸此字段时是否删除字段中的值。

Text Input Traits:文本输入特征。

四、UIImageView

iPhone开发常用控件

常用属性:

image:指定图像文件

Mode:图像在视图内部的对齐方式以及是否缩放图像以适应视图。选择任何图像缩放的选项都会潜在地增加处理开销,因此***避开这些选项,并在导入图像之前调整好图像大小。通常Mode属性为Center。

Alpha:图像透明度。一般设置为1.0

Background:该属性继承自UIView,但它不会影响图像视图的外观,请忽略此属性。

Drawing复选框:选中Opaque表示视图后面的任何内容都不应该绘制,并且允许iPhone都绘图方法通过一些优化来加速绘图。

Clear Context Before Drawing:选中它之后,iPhone将使用透明黑色绘制控件覆盖都所有区域,然后才实际绘制控件。考虑到性能问题,并且适用情况很少,通常很少需要选中ClearContext Before Drawing。

Interaction复选框:

User Interaction Enabled:指定用户能否对此对象进行操作。

Multiple Touch:是否能够接收多点触摸事件。

五、UISlider(滑块)

iPhone开发常用控件

常用属性:Value Changed

示例:

  1. // 将silder的值反映到sliderLabel   
  2. - (IBAction) sliderValueChanged: (id)sender   
  3. {   
  4.     UISlider *slider = (UISlider *)sender;   
  5.     int progressAsInt = (int)(slider.value + 0.5f);   
  6.     NSString *newText = [[NSString alloc] initWithFormat:@"%d", progressAsInt];   
  7.     sliderLabel.text = newText;   
  8.     [newText release];   
  9. }  

六、UISwitch(开关)

iPhone开发常用控件

代码

  1. // 属性on:获取开关的状态是否为on// 方法setOn:设置开关的状态  
  2. - (IBAction) switchChanged: (id)sender{      
  3. UISwitch *whichSwitch = (UISwitch *)sender;      
  4. BOOL setting = whichSwitch.on;      
  5. [leftSwitch setOn:setting animated:YES];      
  6. [rightSwitch setOn:setting animated:YES];  

七、UISegmentedControl

iPhone开发常用控件

  1. #define kSegmentIndex_Switches   0  
  2. #define kSegmentIndex_Button  1  
  3. - (IBAction) segmentChanged: (id)sender{     
  4.  switch ([sender selectedSegmentIndex])     {        
  5.    case kSegmentIndex_Switches:             
  6.     leftSwitch.hidden = NO;              
  7.     rightSwitch.hidden = NO;              
  8.     doSomethingButton.hidden = YES;              
  9.     break;          
  10.     case kSegmentIndex_Button: leftSwitch.hidden = YES;              
  11.     rightSwitch.hidden = YES;             
  12.      doSomethingButton.hidden = NO;  break;     
  13.   }  

八、UIActionSheet(操作表)和UIAlertView(警报)

UIActionSheet用于迫使用户在两个或更多选项之间进行选择都模式视图。操作表从屏幕底部弹出,显示一系列按钮供用户选择,用户只有单击了一个按钮后才能继续使用使用应用程序。

UIAlertView(警报)以蓝色圆角矩形都形式出现在屏幕的中部,警报可显示一个或多个按钮。

为了让控制器类充当操作表的委托,控制器类需要遵从UIActionSheetDelegate协议。我们通过在类声明都超类之后都尖括号中添加协议名称来实现。

  1. @interface UntitledViewController : UIViewController      
  2. <UIActionSheetDelegate>{    // ....}// 创建操作表:  
  3. - (IBAction) buttonPressed: (id)sender{     
  4.  UIActionSheet *actionSheet = [[UIActionSheet alloc]     
  5.  initWithTitle:@"Are you sure?" delegate:self    
  6.          cancelButtonTitle:@"Cancel"  
  7.          destructiveButtonTitle:@"Yes,I'm sure."                       
  8.                       otherButtonTitles:nil];  
  9.    [actionSheet showInView:self.view];      
  10.    [actionSheet release];}// 实现方法:#pragma mark ActionSheet Delegate Methods  
  11.    - (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{      
  12.       if (buttonIndex != [actionSheet cancelButtonIndex])    {        
  13.         NSString *text = [[NSString alloc] initWithFormat:@"test alert"];          
  14.         UIAlertView *alert = [[UIAlertView alloc]    
  15.         initWithTitle:@"Something was done."   
  16.         message:text   delegate:self   cancelButtonTitle:@"OK!" ,otherButtonTitles:nil];          
  17.         [alert show];          
  18.         [alert release];          
  19.         [text release];     
  20.      }  
  21.  }//- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex//{//    NSLog(@"%d",buttonIndex);//} 

示例

视图有一个UISegmentedControl,"Switches"下有两个UISwitch

iPhone开发常用控件

"Button"下有一个“Do Something"的UIButton

iPhone开发常用控件

触摸"Do Something"Button时弹出UIActionSheet

iPhone开发常用控件

触摸选择"Yes,I'm sure."时弹出 UIAlertView

iPhone开发常用控件

  1. OBJECTIVE-C CODE   :UntitledViewController.h   
  2.  //  
  3. //  UntitledViewController.h  
  4. //  Untitled  
  5. //  
  6. //  Created by Elf Sundae on 11/10/10.  
  7. //  Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.  
  8. //  
  9.  
  10. #import <UIKit/UIKit.h> 
  11.  
  12. #define kSegmentIndex_Switches  0  
  13. #define kSegmentIndex_Button      1  
  14.  
  15.  
  16. @interface UntitledViewController : UIViewController  
  17.  <UIActionSheetDelegate> 
  18. {  
  19.  UISwitch * leftSwitch;  
  20.  UISwitch * rightSwitch;  
  21.  UIButton * doSomethingButton;  
  22. }  
  23.  
  24. @property (retain, nonatomic) IBOutlet UISwitch *leftSwitch;  
  25. @property (retain, nonatomic) IBOutlet UISwitch *rightSwitch;  
  26. @property (retain, nonatomic) IBOutlet UIButton *doSomethingButton;  
  27.  
  28. - (IBAction) switchChanged: (id)sender;  
  29. - (IBAction) segmentChanged: (id)sender;  
  30. - (IBAction) buttonPressed: (id)sender;  
  31.  
  32. @end  
  33.  
  34. //  
  35. //  UntitledViewController.h  
  36. //  Untitled  
  37. //  
  38. //  Created by Elf Sundae on 11/10/10.  
  39. //  Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.  
  40. //  
  41. #import <UIKit/UIKit.h> 
  42. #define kSegmentIndex_Switches  0  
  43. #define kSegmentIndex_Button  1  
  44. @interface UntitledViewController : UIViewController  
  45.  <UIActionSheetDelegate> 
  46. {  
  47.  UISwitch * leftSwitch;  
  48.  UISwitch * rightSwitch;  
  49.  UIButton * doSomethingButton;  
  50. }  
  51. @property (retain, nonatomic) IBOutlet UISwitch *leftSwitch;  
  52. @property (retain, nonatomic) IBOutlet UISwitch *rightSwitch;  
  53. @property (retain, nonatomic) IBOutlet UIButton *doSomethingButton;  
  54. - (IBAction) switchChanged: (id)sender;  
  55. - (IBAction) segmentChanged: (id)sender;  
  56. - (IBAction) buttonPressed: (id)sender;  
  57. @end  
  58.  
  59. OBJECTIVE-C CODE   :UntitledViewController.m   
  60.  //  
  61. //  UntitledViewController.m  
  62. //  Untitled  
  63. //  
  64. //  Created by Elf Sundae on 11/10/10.  
  65. //  Copyright 2010 www.cnBlogs.com/ElfSundae. All rights reserved.  
  66. //  
  67.  
  68. #import "UntitledViewController.h"  
  69.  
  70. @implementation UntitledViewController  
  71.  
  72. @synthesize leftSwitch;@synthesize rightSwitch;@synthesize doSomethingButton;// 属性on:获取开关的状态是否为on  
  73. // 方法setOn:设置开关的状态  
  74. - (IBAction) switchChanged: (id)sender{ UISwitch *whichSwitch = (UISwitch *)sender;   
  75. BOOL setting = whichSwitch.on;   
  76. [leftSwitch setOn:setting animated:YES];   
  77. [rightSwitch setOn:setting animated:YES];  
  78. }  
  79. - (IBAction) segmentChanged: (id)sender{  
  80.  switch ([sender selectedSegmentIndex])  {    
  81.  case kSegmentIndex_Switches:  
  82.    leftSwitch.hidden = NO;     
  83.    rightSwitch.hidden = NO;     
  84.    doSomethingButton.hidden = YES;     
  85.    break;    
  86.    case kSegmentIndex_Button:  
  87.    leftSwitch.hidden = YES;     
  88.    rightSwitch.hidden = YES;     
  89.    doSomethingButton.hidden = NO;   
  90.    break;    
  91. }  
  92. }  
  93. - (IBAction) buttonPressed: (id)sender{ UIActionSheet *actionSheet = [[UIActionSheet alloc]   
  94.  initWithTitle:@"Are you sure?"   
  95.       delegate:self  cancelButtonTitle:@"Cancel"     
  96.       destructiveButtonTitle:@"Yes,I'm sure."  otherButtonTitles:nil];    
  97.       [actionSheet showInView:self.view];   
  98.       [actionSheet release];  
  99.     }  
  100.   - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview.  
  101.  [super didReceiveMemoryWarning];  
  102.  // Release any cached data, images, etc that aren't in use.  
  103. }  
  104.  
  105. - (void)viewDidUnload { // Release any retained subviews of the main view.  
  106.  // e.g. self.myOutlet = nil;  
  107.  self.leftSwitch = nil;   
  108.  self.rightSwitch = nil;   
  109.  self.doSomethingButton = nil;   
  110.  [super viewDidUnload];  
  111. }  
  112.  - (void)dealloc {   
  113.  [leftSwitch release];   
  114.  [rightSwitch release];   
  115.  [doSomethingButton release];   
  116.  [super dealloc];  
  117. }  
  118. #pragma mark   
  119. #pragma mark ActionSheet Delegate Methods  
  120. - (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{    
  121. if (buttonIndex != [actionSheet cancelButtonIndex]) {   
  122.  NSString *text = [[NSString alloc] initWithFormat:@"test alert"];   
  123.     UIAlertView *alert = [[UIAlertView alloc]     
  124.     initWithTitle:@"Something was done." message:text     
  125.     delegate:self            
  126.      cancelButtonTitle:@"OK!"  otherButtonTitles:nil];    
  127.      [alert show];    
  128.      [alert release];    
  129.      [text release];   
  130.    }  
  131. }//- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
  132. //{  
  133. // NSLog(@"%d",buttonIndex);  
  134. //}  
  135. @end 

小结:iPhone开发常用控件:UIActionSheet和UIAlertView学习的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-01 18:44:16

iPhone开发 UIALertVie UIActionSh

2011-08-22 14:31:53

iPhone开发

2011-07-29 14:48:48

iPhone开发

2012-04-26 13:23:31

iPhone程序画面控件调整

2011-08-08 10:10:14

iPhone开发 图片 方法

2011-08-01 18:27:58

iPhone开发 UISearchBa

2011-08-15 10:06:22

iPhone开发nib 文件

2011-08-09 17:29:29

iPhone文件屏幕

2013-04-17 11:00:17

Windows PhoWindows Pho

2013-04-17 11:10:02

Windows PhoWindows Pho

2011-08-08 14:57:46

iPhone Autoreleas Property

2011-08-18 10:39:46

iPhone开发界面

2011-08-05 14:48:06

iPhone应用 异步队列

2011-08-09 14:54:29

iPhoneNSDateanotherDate

2011-07-18 14:33:32

2011-08-08 15:56:18

iPhone 震动 NSUserDefa

2011-08-15 17:38:48

iPhone开发调试工具

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-07-27 11:14:37

iPhone UITableVie

2011-07-27 16:46:04

iPhone iPhone破解 MacPort
点赞
收藏

51CTO技术栈公众号