UIActionSheet使用方法


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

这样会生成一个有三个按钮的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

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

遇到过同样的问题
你调用– 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 不是很确定。

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


代码如下:

// 创建时不指定按钮  
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"   delegate:self  
                                              cancelButtonTitle:nil  
                                         destructiveButtonTitle:nil  
                                              otherButtonTitles:nil];  
    // 逐个添加按钮(比如可以是数组循环)  
    [sheet addButtonWithTitle:@"Item A"];  
    [sheet addButtonWithTitle:@"Item B"];  
    [sheet addButtonWithTitle:@"Item C"];  
      
    // 同时添加一个取消按钮  
    [sheet addButtonWithTitle:@"Cancel"];  
    // 将取消按钮的index设置成我们刚添加的那个按钮,这样在delegate中就可以知道是那个按钮  
    sheet.cancelButtonIndex = sheet.numberOfButtons-1;  
    [sheet showFromRect:view.bounds inView:view animated:YES];  
    [sheet release];
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
{  
    if (buttonIndex == actionSheet.cancelButtonIndex)  
    { return; }  
    switch (buttonIndex)  
    {  
        case 0: {  
            NSLog(@"Item A Selected");  
            break;  
        }  
        case 1: {  
            NSLog(@"Item B Selected");  
            break;  
        }  
        case 2: {  
            NSLog(@"Item C Selected");  
            break;  
        }  
    }  
}

转载于:https://my.oschina.net/kevinvane/blog/129766

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值