Iphone之UIAlertView和UIActionSheet

UIAlertView和UIActionSheet的用法很简单,直接看代码:

1.UIAlertView:

.h头文件:

#import <UIKit/UIKit.h>


@interface DialogControl : UIViewController<UIAlertViewDelegate> {//注意要实现这个协议
    UIAlertView *alertView1;//多个dialog的点击事件需要区分是哪一个dialog
    UIAlertView * al;
}
-(IBAction) showDialog;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;//重写点击事件方法
@end

.m文件中的方法:

//点击界面上的一个button促发的方法
-(IBAction)showDialog{
    alertView1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注意增加一个确定按钮的点击事件" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView1 show];
    
    
    //[alertView release];
}
//重写协议中对话筐按钮点击事件方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView == alertView1) {
        if (buttonIndex == 1) {
            al = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你按下了确定按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [al show];
        }
    }
    else if(alertView == al){
        if (buttonIndex == 0) {
            NSLog(@"你点击了确定按钮");
        }
    }
    
}
2.UIActionSheet:

.h头文件:

#import <UIKit/UIKit.h>


@interface OtherView : UIViewController<UIActionSheetDelegate> {//注意实现这个协议
    
}

@end
.m文件的方法:

//进入页面就调用的方法,进入页面就弹出
-(void) viewDidAppear:(BOOL)animated{
    UIActionSheet *actionSeet = [[UIActionSheet alloc]initWithTitle:@"选择事件" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"destructivebutton" otherButtonTitles:@"one",@"two",@"three", nil];
    [actionSeet showInView:self.view];
    [actionSeet release];
}

//重写协议中actionsheet中按钮点击方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%d",buttonIndex);
}




你可能感兴趣的:(iPhone,dialog,button,interface)