IOS系列一:UIAlertView用法

一、基础用法


[[self window] makeKeyAndVisible];

    

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alter show];

    [alter release];

    

    return YES;



二、提示有多个按钮


[[self window] makeKeyAndVisible];

    

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];

    [alter show];

    [alter release];

    

    return YES;


三、判断点了哪一个按钮(通过alter的委托)


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{


    [[self window] makeKeyAndVisible];

    

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];

    [alert show];

    [alert release];

    

    return YES;

}


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSString *msg = [[NSString alloc] initWithFormat:@"你点了第%d个按钮",buttonIndex];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alert show];

    [alert release];

}


四、手动取消对话框


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{


    [[self window] makeKeyAndVisible];

    

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];

    [alert show];

    [alert release];

    

    return YES;

}


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSString *msg = [[NSString alloc] initWithFormat:@"你点了第%d个按钮",buttonIndex];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alert show];

    

    [alert dismissWithClickedButtonIndex:0 animated:YES];

    

    [alert release];

}


五、添加子视图


在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{


    [[self window] makeKeyAndVisible];

    /*

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];

    [alert show];

    [alert release];

    */

    

    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView添加自定义视图"

                                                  message:nil

                                                 delegate:nil

                                        cancelButtonTitle:nil

                                        otherButtonTitles:nil];

    [alert show];

    

    UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);

    [activeView startAnimating];

    [alert addSubview:activeView];

    [activeView release];

    

    [alert release];


    

    return YES;

}




六、修改展现属性


UIAlertView默认情况下所有的text是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK还是很灵活的, 有很多delegate消息供调用程序使用


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{


    [[self window] makeKeyAndVisible];

    /*

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不好啦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"fuck1",@"fuck2",@"fuck3",@"fuck4", nil];

    [alert show];

    [alert release];

    */

    

    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"添加自定义视图"

                                                  message:nil

                                                 delegate:self

                                        cancelButtonTitle:nil

                                        otherButtonTitles:nil];

    [alert show];

    

    UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);

    [activeView startAnimating];

    [alert addSubview:activeView];

    [activeView release];

    

    [alert release];


    

    return YES;

}


-(void) willPresentAlertView:(UIAlertView *)alertView

{

    for( UIView * view in alertView.subviews )

    {

        if( [view isKindOfClass:[UILabel class]] )

        {

            UILabel* label = (UILabel*) view;

            label.textAlignment=UITextAlignmentLeft;

        }

    }

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值