iOS开发-UIAlerView

来源:互联网 发布:ubuntu14.04安装mysql 编辑:程序博客网 时间:2024/04/26 02:58

警告提示视图

复制代码
NSString *content = NSLocalizedString(@"this is test alert message", nil);UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert title", nil)                                                    message:content                                                   delegate:self                                          cancelButtonTitle:NSLocalizedString(@"cancel", nil)                                          otherButtonTitles:NSLocalizedString(@"ok", nil), nil];    alert.tag = 1;  //用来作为标识,可区分不同的提示框    [alert show];
复制代码

 

在使用的类里,实现代理协议UIAlertViewDelegate

按钮点击处理方法

复制代码
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  switch(buttonIndex){    case 0:      //do cancel      break;    case 1:      //do ok      break;   }}
复制代码

 

在UIAlertView显示动画之前,改变提示message的对其方式,改为左对齐

复制代码
- (void)willPresentAlertView:(UIAlertView *)alertView{  //遍历UIAlertView的子视图,改变label的大小,尺寸,和对齐方式    for(UIView *subview in alertView.subviews)    {        if([[subview class] isSubclassOfClass:[UILabel class]])        {            if(![((UILabel *)subview).text isEqualToString:iGexinLocalizedString(@"file detail", nil)]){                                UILabel *label = (UILabel*)subview;                CGRect rect = CGRectInset(label.frame, 20, 0);  //label边界留20                NSString * labelText = label.text;                CGFloat oldH = rect.size.height;                rect.size = [labelText sizeWithFont:label.font constrainedToSize:                                  CGSizeMake(rect.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; //计算文本的宽度和高度                if(oldH < rect.size.height){                    rect.origin.y -= 10;                }                label.frame = rect;                    //调整label的位置和尺寸                label.textAlignment = UITextAlignmentLeft;      //label文本左对齐                break;            }        }    }}
复制代码
原创粉丝点击