UIActionSheet

iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择。当用户将要进行的操作具有一定危险时,常常使用Action Sheet对用户进行危险提示,这样,用户有机会进行取消操作。

Alert相当于Windows中的Messagebox,跟Action Sheet也是类似的。不同的是,Alert可以只有一个选择项,而Action Sheet却至少要两个选项。

跟以往一样,假设我们已经建立了一个Single View Application,打开其中的ViewController.xib文件。

首先,我们先放一个Button在View上面,我们要的效果是:点击Button打开一个Action Sheet,接下来点击Action Sheet的一个按钮,弹出一个Alert。

1、首先,要在ViewController.h中添加代码,使其实现一个协议。添加代码的地方在@interface那行的最后添加,添加之后那行代码是:

@interface ViewController : UIViewController

2、拖放一个Button到View上,将Button的名称改为 Do something。

3、为这个Button建立Action映射,映射到ViewController.h中,事件类型默认,名称为 buttonPressed。

4、在ViewController.m中找到buttonPressed方法,添加以下代码:

- (IBAction)buttonPressed:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
        initWithTitle:@"Are you sure?"
        delegate:self
        cancelButtonTitle:@"No Way!"
        destructiveButtonTitle:@"Yes, I'm sure!"
        otherButtonTitles:nil];
    [actionSheet showInView:self.view];
}

如上面代码所示,创建一个Action Sheet需要多个参数:

(1)initWithTitle:设置标题,将会显示在Action Sheet的顶部

(2)delegate:设置Action Sheet的委托。当Action Sheet的一个按钮被按下后,它的delegate将会被通知,并且会执行这个delegate的actionSheet: didDismissWithButtonIndex方法将会执行。这里,我们将delegate设成self,这样可以保证执行我们自己在 ViewController.m写的actionSheet: didDismissWithButtonIndex方法

(3)cancelButtonTitle:设置取消按钮的标题,这个取消按钮将会显示在Action Sheet的最下边

(4)destructiveButtonTitle:设置第一个确定按钮的标题,这个按钮可以理解成:"好的,继续"

(5)otherButtonTitles:可以设置任意多的确定按钮,想要添加两个按钮,可以写成:

otherButtonTitles: @”New Button 1”, @”New Button 2”, nil

注意到,最后一个参数要是nil

[actionSheet showInView:self.view]这条语句用来显示Action Sheet,准确的说,这条语句是给这个Action Sheet设置Parent,而这个Parent必须是一个View,并且是当前正在显示的View。

5、然后,我们在ViewController.m中添加一个方法,完整代码为:

- (void)actionSheet:(UIActionSheet *)actionSheet 
               didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSString *msg = nil;
        msg = @"You can breathe easy, everything went OK.";
        UIAlertView *alert = [[UIAlertView alloc]
            initWithTitle:@"Something was done"
            message:msg
            delegate:self
            cancelButtonTitle:@"Prew!"
            otherButtonTitles: nil];
        [alert show];
    }
}

这个方法就是我们轻触了Action Sheet之后将会执行的代码。由于之前我们将Action Sheet的delegate设成self,因而这个方法将会被调用,这个方法的参数buttonIndex表示用户所轻触的按钮的编号,按钮编号是从上到下,从0开始的,例如,"Yes, I'm sure!"这个按钮的编号是0,因为它是第一个确定按钮,取消按钮是显示在最下边的。取消按钮的编号,可以通过[actionSheet cancelButtonIndex]直接获得。

构造一个Alert也要填写很多参数:

(1)initWithTitle:设置标题,将会显示在Alert的顶部

(2)message:设置提示消息内容

(3)delegate:设置Alert的委托。这里,我们设成self

(4)cancelButtonTitle:设置取消按钮的标题

(5)otherButtonTitles:与Action Sheet类似

[alert show]这条语句用来显示Alert。

 

自定义UIALertView

 

Ios代码 复制代码 收藏代码
  1. #import <UIKit/UIKit.h>
  2. @interface CustomPickerView : UIAlertView <UIPickerViewDataSource, UIPickerViewDelegate> {
  3. NSArray *majorNames;
  4. NSArray *grades;
  5. UIPickerView *selectPicker;
  6. int selectedMajor;
  7. int selectedGrade;
  8. }
  9. @end

实现文件:

Ios代码  复制代码  收藏代码
  1. #import "CustomPickerView.h"
  2. #define componentCount 2
  3. #define majorComponent 0
  4. #define gradeComponent 1
  5. #define majorComponentWidth 165
  6. #define gradeComponentWidth 70
  7. @implementation CustomPickerView
  8. - (id)initWithFrame:(CGRect)frame {
  9. if (self = [super initWithFrame:frame]) {
  10. majorNames = [[NSArray alloc]initWithObjects:@"111", @"222", @"333", @"444", @"555", nil];
  11. grades = [[NSArray alloc]initWithObjects:@"aaa", @"bbb", @"ccc", @"ddd", @"eee", nil];
  12. selectPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
  13. selectPicker.showsSelectionIndicator = YES;
  14. selectPicker.delegate = self;
  15. selectPicker.dataSource = self;
  16. selectPicker.opaque = YES;
  17. [self addSubview:selectPicker];
  18. }
  19. return self;
  20. }
  21. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  22. return componentCount;
  23. }
  24. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  25. if (component == majorComponent) {
  26. return [majorNames count];
  27. } else {
  28. return [grades count];
  29. }
  30. }
  31. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  32. UILabel *printString;
  33. if (component == majorComponent) {
  34. printString = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, majorComponentWidth, 45)];
  35. printString.text = [majorNames objectAtIndex:row];
  36. [printString setFont:[UIFont fontWithName:@"Georgia" size:12.0f]];
  37. } else {
  38. printString = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, gradeComponentWidth, 45)];
  39. printString.text = [grades objectAtIndex:row];
  40. }
  41. [printString autorelease];
  42. printString.backgroundColor = [UIColor clearColor];
  43. printString.textAlignment = UITextAlignmentCenter;
  44. return printString;
  45. }
  46. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
  47. return 45.0;
  48. }
  49. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
  50. if (component == majorComponent) {
  51. return majorComponentWidth;
  52. } else {
  53. return gradeComponentWidth;
  54. }
  55. }
  56. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  57. selectedMajor = [pickerView selectedRowInComponent:majorComponent];
  58. selectedGrade = [pickerView selectedRowInComponent:gradeComponent];
  59. }
  60. - (void)setFrame:(CGRect)rect {
  61. [super setFrame:CGRectMake(0, 0, rect.size.width, 330)];
  62. self.center = CGPointMake(320/2, 480/2);
  63. }
  64. - (void)layoutSubviews {
  65. selectPicker.frame = CGRectMake(10, 45, self.frame.size.width - 52, self.frame.size.height -50);
  66. for (UIView *view in self.subviews) {
  67. if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) {
  68. view.frame = CGRectMake(view.frame.origin.x, self.bounds.size.height - view.frame.size.height - 15, view.frame.size.width, view.frame.size.height);
  69. }
  70. }
  71. }
  72. - (void)dealloc {
  73. [super dealloc];
  74. }
  75. @end

示例:

Ios代码  复制代码  收藏代码
  1. CustomPickerView *alert = [[CustomPickerView alloc]
  2. initWithTitle:@"Orange"
  3. message:nil
  4. delegate:self
  5. cancelButtonTitle:@"Cancel"
  6. otherButtonTitles:@"Submit", nil];
  7. [alert show];
  8. [alert release];


  转http://blog.sina.com.cn/s/blog_5fb39f91010179zc.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值