UIActionSheet使用方法

以下代码是自己为了测试而写的,不是转的别人的代码。但是由于下面的很多东西都是看的别人的一些博客汇总而成,故列为转载,于是就不写转载自哪里了。  

 UIActionSheet *actionSheet = [[UIActionSheet alloc]

           initWithTitle:@"actionSheet 实例?"
           delegate:self
           cancelButtonTitle:@"取消"
           destructiveButtonTitle:@"确定"
           otherButtonTitles:@"其他按钮",nil];
  [actionSheet showInView:[self view]];

  [actionSheet release];

这样会生成一个有三个按钮的actionSheet表单,确定按钮,其他,取消按钮,分别的按钮索引为0,1,2.这个索引可以会被委托方法使用。


还可以设置一下此actionSheet的一些特性:

    actionSheet.destructiveButtonIndex=0;//设置那个红色的提醒按钮是哪一个,默认是上面的确定按钮

    actionSheet.tag = TAG_MY_ACTIONSHEET;//设置actionSheet的tag标记,便于在同一个delegate方法区分不同的actionSheet

    actionSheet.actionSheetStyle =UIActionSheetStyleDefault;//设置不同的actionSheet风格,此为灰色背景,白字。建议自己测试一下想要的风格


有几个delegate方法,最常用的肯定是按下按钮的了:

#pragma mark - 

#pragma mark actionSheet delegate 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

        switch (buttonIndex) {

            case 0:

                NSLog(@"click at index %d,确定操作", buttonIndex);

                break;

            case 1:

                NSLog(@"click at index %d,其他操作", buttonIndex);

                break;

            case 2:

                NSLog(@"click at index %d,取消操作", buttonIndex);

                break;

            default:

                NSLog(@"unknown click at index %d", buttonIndex);

                break;

        }

    }

}实际上还有其他的一些委托方法,我没有测试,就没写,需要自己去测试了。


上面的actionSheet可能会有按钮按的时候不容易按下。转自:http://blog.csdn.net/zcl369369/article/details/7515069

如图,只有在点标出的红框才行,点别的地方都没反应,在模拟器和真机上都是这样

UIActionSheet使用方法_第1张图片


遇到过同样的问题
你调用– showInView: 时传入的View 有问题,那个View的区域就不包括底下的TabBarController

你的 view size 不够. 或者你显示的方式不对. 比如你页面下方有一个 tabbar, 但是你显示是 showInView 有可能会出现这中情况,  改为 showFromTabbar .


恩,是被tabbar挡住了
解决:[actionSheet showInView:[UIApplication sharedApplication].keyWindow];

我的是有一个tabbar,我用的办法: [alert showFromTabBar : self . tabBarController . tabBar ];



取消按钮位置设定,自己没有测试,方便自己以后用的时候。转载自:http://blog.csdn.net/qhexin/article/details/7066341

使用 UIActionsheet 的时候,如果otherButton太多的话,取消按钮会排版到最下面,如果otherButton很少的话,取消按钮又排版在上面。

这样点击按钮时我们收到的按钮index 不是很确定。

所以干脆就让取消按钮一直在下面。


代码如下:

[cpp]  view plain copy
  1. <pre style="color: rgb(102, 102, 102); font-size: 14px; background-color: rgb(221, 221, 221); ">// 创建时不指定按钮  
  2.     UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"   delegate:self  
  3.                                               cancelButtonTitle:nil  
  4.                                          destructiveButtonTitle:nil  
  5.                                               otherButtonTitles:nil];  
  6.     // 逐个添加按钮(比如可以是数组循环)  
  7.     [sheet addButtonWithTitle:@"Item A"];  
  8.     [sheet addButtonWithTitle:@"Item B"];  
  9.     [sheet addButtonWithTitle:@"Item C"];  
  10.       
  11.     // 同时添加一个取消按钮  
  12.     [sheet addButtonWithTitle:@"Cancel"];  
  13.     // 将取消按钮的index设置成我们刚添加的那个按钮,这样在delegate中就可以知道是那个按钮  
  14.     sheet.cancelButtonIndex = sheet.numberOfButtons-1;  
  15.     [sheet showFromRect:view.bounds inView:view animated:YES];  
  16.     [sheet release];</pre>  
  17. <pre></pre>  
  18. <p></p>  
  19. <pre></pre>  
  20. <p></p>  
  21. <p></p>  
  22. <p></p><pre name="code" class="java">- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
  23. {  
  24.     if (buttonIndex == actionSheet.cancelButtonIndex)  
  25.     { return; }  
  26.     switch (buttonIndex)  
  27.     {  
  28.         case 0: {  
  29.             NSLog(@"Item A Selected");  
  30.             break;  
  31.         }  
  32.         case 1: {  
  33.             NSLog(@"Item B Selected");  
  34.             break;  
  35.         }  
  36.         case 2: {  
  37.             NSLog(@"Item C Selected");  
  38.             break;  
  39.         }  
  40.     }  
  41. }</pre><br>  
  42. <br>  
  43. <p></p>  
  44. <p></p>  
  45. <p><br>  
  46. </p>  
  47. <pre></pre>  

你可能感兴趣的:(测试)