首页 > 其他 > 详细

IOS 学习之UIActionSheet的使用

时间:2014-07-22 23:10:44      阅读:454      评论:0      收藏:0      [点我收藏+]

UIActionSheet 是IOS中从底部弹出的按钮选项,可以为每一个选择设置触发事件。

为了快速完成这个例子我们在xcode中建立一个singleview 项目,在窗口中添加一个button用来弹出actionsheet

1.首先在头文件中声明协议<UIActionSheetDelegate>。

1
2
3
4
5
6
#import <UIKit/UIKit.h>
 
@interface sheetviewViewController : UIViewController<UIActionSheetDelegate>
- (IBAction)showSheet:(id)sender;
 
@end

 2.添加button。

3.为button建立action连线。

4.在m文件上添加事件代码,弹出actionsheet

1
2
3
4
5
6
7
8
9
10
- (IBAction)showSheet:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"title,nil时不显示"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"确定"
                                  otherButtonTitles:@"第一项", @"第二项",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

5.设置actionsheet的样式。

actionsheet.actionSheetStyle=UIActionSheetStyleBlackOpaque /UIActionSheetStyleAutomatic /UIActionSheetStyleDefault                               /UIActionSheetStyleBlackTranslucent /UIActionSheetStyleBlackOpaque

6.显示actionsheet

调用showInView方法

7.响应actionsheet方法

1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self showAlert:@"确定"];
    }else if (buttonIndex == 1) {
        [self showAlert:@"第一项"];
    }else if(buttonIndex == 2) {
        [self showAlert:@"第二项"];
    }else if(buttonIndex == 3) {
        [self showAlert:@"取消"];
    }
 
}

 8.注意事项

在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有。解决办法:

在 showView时这样使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];这样就不会发生遮挡现象了。

IOS 学习之UIActionSheet的使用

原文:http://www.cnblogs.com/jackwuyongxing/p/3514780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!