[iPhone开发之控件的使用]UIActionSheet的各种属性、方法及代理的使用

2023-05-20,,

[c-sharp] view plaincopy

  1. #import "ActionSheetTestViewController.h"  
  2. @implementation ActionSheetTestViewController  
  3. /* 
  4. Tasks 
  5.   
  6. Creating Action Sheets 
  7.     – initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:   
  8.     Setting Properties 
  9.     delegate  property   
  10.     title  property   
  11.     visible  property   
  12.     actionSheetStyle  property  无例 
  13. Configuring Buttons 
  14.     – addButtonWithTitle:   
  15.     numberOfButtons  property   
  16.     – buttonTitleAtIndex:   
  17.     cancelButtonIndex  property   
  18.     destructiveButtonIndex  property   
  19.     firstOtherButtonIndex  property   
  20. Displaying 
  21.     – showFromTabBar:   
  22.     – showFromToolbar:   
  23.     – showInView:   
  24. Dismissing 
  25.     – dismissWithClickedButtonIndex:animated:   
  26. */  
  27.   
  28. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  29. - (void)viewDidLoad {  
  30.     UILabel *numOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 10.0, 30.0, 30.0)];  
  31.     UILabel *titleOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 10.0, 100.0, 30.0)];  
  32.     UILabel *cancelBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(200.0, 10.0, 30.0, 30.0)];  
  33.     UILabel *destructiveBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 50.0, 30.0, 30.0)];  
  34.     UILabel *firstOtherBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 50.0, 30.0, 30.0)];  
  35.     UIActionSheet *actionSheetTest = [[UIActionSheet alloc]initWithTitle:@"ActionSheetTest"   
  36.                                 delegate:self  
  37.                                 cancelButtonTitle:@"CancelButton"   
  38.                                 destructiveButtonTitle:@"RedButton"   
  39.                                 otherButtonTitles:@"OtherButton1",@"OtherButton2",nil];  
  40.     //看actionSheet是否可见,这是一个只读属性  
  41.     BOOL a = actionSheetTest.visible;  
  42.     NSLog(@"%d",a);  
  43.       
  44.     //不考虑指定索引的按钮的动作,可以设置是否有动画  
  45.     [actionSheetTest dismissWithClickedButtonIndex:0 animated:NO];  
  46.       
  47.     //设置标题  
  48.     actionSheetTest.title = @"ActionSheetTitle";  
  49.       
  50.     //通过给定标题添加按钮  
  51.     [actionSheetTest addButtonWithTitle:@"addButtonWithTitle"];  
  52.       
  53.     //按钮总数  
  54.     numOfBtn.text = [NSString stringWithFormat:@"%d",actionSheetTest.numberOfButtons];  
  55.       
  56.     //获取指定索引的标题  
  57.     titleOfBtn.text = [actionSheetTest buttonTitleAtIndex:4];  
  58.       
  59.     //获取取消按钮的索引  
  60.     cancelBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.cancelButtonIndex];  
  61.       
  62.     //获取红色按钮的索引  
  63.     destructiveBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.destructiveButtonIndex];  
  64.       
  65.     //获取第一个其他按钮的索引  
  66.     firstOtherBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.firstOtherButtonIndex];  
  67.       
  68.     //设置actionSheet出现的方式  
  69.     [actionSheetTest showInView:self.view];//or [actionSheetTest showFromTabBar:] or [actionSheetTest showFromToolBar:]  
  70.       
  71.     [self.view addSubview:numOfBtn];  
  72.     [self.view addSubview:titleOfBtn];  
  73.     [self.view addSubview:cancelBtnIndex];  
  74.     [self.view addSubview:destructiveBtnIndex];  
  75.     [self.view addSubview:firstOtherBtnIndex];  
  76.       
  77.     [actionSheetTest release];  
  78.     [numOfBtn release];  
  79.     [titleOfBtn release];  
  80.     [cancelBtnIndex release];  
  81.     [destructiveBtnIndex release];  
  82.     [firstOtherBtnIndex release];  
  83.       
  84.     [super viewDidLoad];  
  85. }  
  86.   
  87. /* 
  88. // Override to allow orientations other than the default portrait orientation. 
  89. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  90.     // Return YES for supported orientations 
  91.     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
  92. } 
  93. */  
  94. - (void)didReceiveMemoryWarning {  
  95.     // Releases the view if it doesn't have a superview.  
  96.     [super didReceiveMemoryWarning];  
  97.       
  98.     // Release any cached data, images, etc that aren't in use.  
  99. }  
  100. - (void)viewDidUnload {  
  101.     // Release any retained subviews of the main view.  
  102.     // e.g. self.myOutlet = nil;  
  103. }  
  104.   
  105. - (void)dealloc {  
  106.     [super dealloc];  
  107. }  
  108.  
  109. #pragma mark -- UIActionSheetDelegate --  
  110. //根据被点击按钮的索引处理点击事件  
  111. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {  
  112.     NSLog(@"clickedButtonAtIndex:%d",buttonIndex);  
  113. }  
  114. //ActionSheet已经消失时  
  115. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {  
  116.     NSLog(@"didDismissWithButtonIndex:%d",buttonIndex);  
  117. }  
  118. //ActionSheet即将消失时  
  119. - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {  
  120.     NSLog(@"willDismissWithButtonIndex:%d",buttonIndex);  
  121. }  
  122. //  
  123. - (void)actionSheetCancel:(UIActionSheet *)actionSheet {  
  124.     NSLog(@"actionSheetCancel");  
  125.       
  126. }  
  127. //ActionSheet已经显示时  
  128. - (void)didPresentActionSheet:(UIActionSheet *)actionSheet {  
  129.     NSLog(@"didPresentActionSheet%@",actionSheet);  
  130. }  
  131. //ActionSheet即将显示时  
  132. - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {  
  133.     NSLog(@"willPresentActionSheet%@",actionSheet);  
  134. }  
  1. @end  



原文转载:http://blog.csdn.net/banyingli/article/details/6167561


《[iPhone开发之控件的使用]UIActionSheet的各种属性、方法及代理的使用.doc》

下载本文的Word格式文档,以方便收藏与打印。