IOS之UIActionSheet& UIAlertView

来源:互联网 发布:计算机速录软件 编辑:程序博客网 时间:2024/06/06 09:01
一、UIActionSheet 
UIActionShee用来对指定的事件向用户呈现一系列的操作;也可以用来提示用户确认有些带有危险性的操作;ActionSheet包含一个可选的标题和一个或多个按钮,其中每一个对应于要执行的操作。
1、声明代理<UIActionSheetDelegate>
2、
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择操作" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"删除" otherButtonTitles:@"取消", nil];   
    // 用tag值来分辨是代理方法调用时哪个actionSheet
    sheet.tag = 1;  
    sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;  
    [sheet showInView:self.view];
     
     
    // 按钮比较多时,将按钮标题放在数组中遍历插入
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: nil];
    actionSheet.tag = 2;
    for(NSString *title in array){
        [actionSheet addButtonWithTitle:title];
    }
    [actionSheet showInView:self.view];


#pragma mark - UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 1) {
        NSLog(@"sheet clicked index = %d",buttonIndex);
    }
    else if(actionSheet.tag == 2){
        NSLog(@"actionSheet clicked index = %d",buttonIndex);
    }
     
}
参数解释: 
与导航栏类似,操作表单也支持三种风格 :
UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字   
UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字   
UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字  


cancelButtonTitle  destructiveButtonTitle是系统自动的两项。
otherButtonTitles是自己定义的项,注意,最后一个参数要是nil:otherButtonTitles:@"第一项", @"第二项",nil;






二、UIAlertView
使用UIAlertView向用户提示一条警告消息,Alert View功能与ActionSheet类似,但是外观上有所不同
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否删除" message:@"删除后不可撤销" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.tag = 1;    
[alert show];


#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1) {
        NSLog(@"you clicked:%@",[alertView buttonTitleAtIndex:buttonIndex]);
    }
}
0 0
原创粉丝点击