IOS开发之UIAlerView

本文详细介绍了在iOS开发中如何使用UIAlertView,包括其代理方法、按钮索引、各种风格的使用,以及如何监听和响应用户操作。通过示例代码展示了如何创建UIAlertView,设置标题、消息、按钮,并实现点击事件的处理。
摘要由CSDN通过智能技术生成
//使用UIAlertViewDelegate方法
//按钮点击后,获取按钮的索引值
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{


    switch (buttonIndex) {
        case 0:
            NSLog(@"您点击了取消按钮");
            break;  
        case 1:
            NSLog(@"您点击了第2个按钮");
            break;
        case 2:
            NSLog(@"您点击了第3个按钮");
            break;
        case 3:
            NSLog(@"您点击了第4个按钮");
            break;
            
        default:
            break;
    }


}
//实现取消按钮事件的监听
-(void)alertViewCancel:(UIAlertView *)alertView{


    NSLog(@"您点击了取消按钮!===============");
}


//响应按钮方法
-(void)showAlert{


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题" 
    message:@"消息内容" 
    delegate:self cancelButtonTitle:@"取消"
     otherButtonTitles:@"Other1",@"Other2",@"Other3", nil];


    [alert show];
    [alert release];
}


















UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View" message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UIAlertView使用全解
标准的双按钮,cancel那个buttonIndex 为0, ok button 的buttonIndex为1


UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];
UIAlertView使用全解
和程序里的顺序一样,cancel   ok   thirdButton 的buttonIndex 分别为0 1 2




UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];
UIAlertView使用全解
同理,cancel   ok   thirdButton FourthButton的buttonIndex 分别为0 1 2 3
[alertView show];


UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
alertView--->这个不用多说了吧
buttonIndex---->从0开始
可以通过if (buttonIndex == 1) { } 这样的来控制点击了某个按钮需要做什么操作


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法在动画结束和视图隐藏之后调用


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法在动画开始和视图隐藏之前调用


- (void)alertViewCancel:(UIAlertView *)alertView
在视图将要被取消之前
例如,用户点击了home键
三个函数的调用顺序依次是:
alertViewCancel----》willDismissWithButtonIndex---》didDismissWithButtonIndex


- (BOOL)alertViewShouldEnableFirstOtherButton(UIAlertView *)alertView
ios 5+
设置yes / no  将会设置alertView 的第一个otherButton的enable属性


- (void)didPresentAlertView:(UIAlertView *)alertView
在视图提交给用户以后调用


-  (void)willPresentAlertView:(UIAlertView *)alertView
在视图提交给用户以前调用


这六个delegate 方法调用的顺序依次是
alertViewShouldEnableFirstOtherButton---->willPresentAlertView--->didPresentAlertView
---->clickedButtonAtIndex---->(如果会触发视图取消,则会调用alertViewCancel)willDismissWithButtonIndex---->didDismissWithButtonIndex


ios4.0以后 alertView不会自动随着程序转向后台而移除
alertView属性
1.alertViewStyle:
UIAlertViewStyleDefault 只弹信息和按钮
UIAlertViewStyleSecureTextInput 有一个textfield加密框
UIAlertViewStylePlainTextInput 有一个不加密的textfield
UIAlertViewStyleLoginAndPasswordInput 有两个textfield,Login和password


只要有textfield就可以用textfieldAtIndex来捕获并进行相应的操作例如换键盘类型


2.cancelButtonIndex
开始是0,如果没有设置cancel button 则是-1


3.delegate
如果没有设置则是nil


4.firstOtherButtonIndex
从0开始,如果没设置则是-1,而且没被设置则会被忽略


5.message 
消息


6.numberOfButtons
只读  alertView中的按钮数量


7.title
标题


8.visible
只读  如果是yes 表示被显示


实例方法
- (NSInteger)addButtonWithTitle:(NSString *)title
返回值是增加的Button的index


- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
输入buttonIndex 返回button的标题


- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
程序自动完成点击buttonIndex的button 并dismiss 整个alertView的操作


- (id)initWithTitle:(NSString *)title message:(NSString)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitles, ...
这个就不用多说了


- (void)show
要显示必须要调用这个alertview才会显示


-  (UITextField *)textfieldAtIndex:(NSInteger)textfieldIndex
返回值是textfield
UIAlertViewStyleDefault 没有
UIAlertViewStyleSecureInput textfieldIndex 只有一个为0
UIAlertViewStylePlainInput textfieldIndex 只有一个为0
UIAlertViewStyleLoginAndPasswordInput textfieldIndex有两个 0 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值