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: didDismissWithButtonInde
(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
- #import <UIKit/UIKit.h>
- @interface CustomPickerView : UIAlertView <UIPickerViewDataSource, UIPickerViewDelegate> {
- NSArray *majorNames;
- NSArray *grades;
- UIPickerView *selectPicker;
- int selectedMajor;
- int selectedGrade;
- }
- @end
实现文件:
- #import "CustomPickerView.h"
- #define componentCount 2
- #define majorComponent 0
- #define gradeComponent 1
- #define majorComponentWidth 165
- #define gradeComponentWidth 70
- @implementation CustomPickerView
- - (id)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- majorNames = [[NSArray alloc]initWithObjects:@"111", @"222", @"333", @"444", @"555", nil];
- grades = [[NSArray alloc]initWithObjects:@"aaa", @"bbb", @"ccc", @"ddd", @"eee", nil];
- selectPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
- selectPicker.showsSelectionIndicator = YES;
- selectPicker.delegate = self;
- selectPicker.dataSource = self;
- selectPicker.opaque = YES;
- [self addSubview:selectPicker];
- }
- return self;
- }
- - (NSInteger)numberOfComponentsInPick
erView:(UIPickerView *)pickerView { - return componentCount;
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
- if (component == majorComponent) {
- return [majorNames count];
- } else {
- return [grades count];
- }
- }
- - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
- UILabel *printString;
- if (component == majorComponent) {
- printString = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, majorComponentWidth, 45)];
- printString.text = [majorNames objectAtIndex:row];
- [printString setFont:[UIFont fontWithName:@"Georgia" size:12.0f]];
- } else {
- printString = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, gradeComponentWidth, 45)];
- printString.text = [grades objectAtIndex:row];
- }
- [printString autorelease];
- printString.backgroundColor = [UIColor clearColor];
- printString.textAlignment = UITextAlignmentCenter;
- return printString;
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
- return 45.0;
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
- if (component == majorComponent) {
- return majorComponentWidth;
- } else {
- return gradeComponentWidth;
- }
- }
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- selectedMajor = [pickerView selectedRowInComponent:majorComponent];
- selectedGrade = [pickerView selectedRowInComponent:gradeComponent];
- }
- - (void)setFrame:(CGRect)rect {
- [super setFrame:CGRectMake(0, 0, rect.size.width, 330)];
- self.center = CGPointMake(320/2, 480/2);
- }
- - (void)layoutSubviews {
- selectPicker.frame = CGRectMake(10, 45, self.frame.size.width - 52, self.frame.size.height -50);
- for (UIView *view in self.subviews) {
- if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) {
- view.frame = CGRectMake(view.frame.origin.x, self.bounds.size.height - view.frame.size.height - 15, view.frame.size.width, view.frame.size.height);
- }
- }
- }
- - (void)dealloc {
- [super dealloc];
- }
- @end
示例:
转自 http://blog.sina.com.cn/s/blog_5fb39f91010179zc.html