UIActionSheet和UIAlertView控件的用法


在IOS开发构成中,弹出对话是经常遇到,就像在Windows系统中“确定--取消”选择对话框一样。这一篇文章拾图网温州网站建设将与大家一起分享一下UIActionSheet控件和UIAlertView控件的使用功能。

一、UIActionSheet行动表控件:

UIActionSheet 用来对指定的事件向用户呈现一系列的操作;也可以用来提示用户确认有些带有危险性的操作;ActionSheet包含一个可选的标题和一个或多个按钮,其中每一个对应于要执行的操作;当用户将要进行的操作具有一定危险时,常常使用Action Sheet对用户进行危险提示,这样,用户有机会进行取消操作。

步骤:

1、声明代理

<UIActionSheetDelegate>

2、初始化加载

// 简单初始化显示
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择操作" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"删除" otherButtonTitles:@"取消", nil];
     
    // 用tag值来分辨是代理方法调用时哪个actionSheet
    sheet.tag = 10;
     
    [sheet showInView:self.view];
     
     
    // 按钮比较多时,将按钮标题放在数组中遍历插入
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: nil];
     
    actionSheet.tag = 100;
     
    for(NSString *title in array){
        [actionSheet addButtonWithTitle:title];
    }
    [actionSheet showInView:self.view];
     
     
    // 在iPad上显示带箭头的actionSheet,在iPhone上只能从底部弹出,如需在iPhone上出现带箭头的形势
    // 从某个位置弹出
    [actionSheet showFromRect:CGRectMake(20, 30, 100, 100) inView:self.view animated:YES];
     
    // 从导航栏右键弹出
    [actionSheet showFromBarButtonItem:self.navigationController.navigationItem.rightBarButtonItem animated:YES];

以上代码展示了三种加载方式(1、简单的;2、多按钮时使用数组插入;2、在iPad中显示带箭头的)

3、实现代理方法

#pragma mark - UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 10) {
        NSLog(@"sheet clicked index = %d",buttonIndex);
    }
    else if(actionSheet.tag == 100){
        NSLog(@"actionSheet clicked index = %d",buttonIndex);
    }
     
}

1.jpg



二、UIAlertView 警告控件

使用UIAlertView控件向用户弹出一条警告消息,Alert View功能与ActionSheet类似,但是外观上有所不同;不同的是,Alert可以只有一个选择项,而Action Sheet却至少要两个选项。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否删除" message:@"删除后不可撤销" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
     
alert.tag = 20;
     
[alert show];
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==20) {
        NSLog(@"you clicked:%@",[alertView buttonTitleAtIndex:buttonIndex]);
    }
}

2.jpg


UIAlertView、UIActionSheet兼容iOS8

iOS8新增了 UIAlertController来代替UIAlertView、UIActionSheet的使用。下面代码在不使用 UIAlertController的情况下,用最简单的方法让 UIAlertView、UIActionSheet兼容iOS8.

一、UIAlertView

iOS8下,为了避免弹出框中的message信息会变成粗体,太过靠近顶部,在UIAlertView初始化的时候title为nil的时,为了保存跟iOS8之前的版本一致,需要在初始化的时候将title置为@""就可以了。代码如下:

//title置为@""兼容iOS8

//title置为@""兼容iOS8
    UIAlertView *delAlert = [[[UIAlertView alloc] initWithTitle:@"" message:@"删除联系人?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil] autorelease];
    [delAlert show];

二、UIActionSheet

iOS8下,为避免ActionSheet表单初始化时title为@""上方会多出一行空白栏,需要在初始化的时候将title置为nil.

UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:nil
                                                              delegate:self
                                                     cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
                                                destructiveButtonTitle:[NSString stringWithFormat:NSLocalizedString(@"call", nil),self.phoneNumber]
                                                     otherButtonTitles:NSLocalizedString(@"add to contact", nil),nil]
                                  autorelease];
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];

以上资料有拾图网收集整理。

注:本文转载自拾图网[http://www.tshinet.com],转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。
上一篇 下一篇

分享

最新评论

最新加入


0.5783s