举例:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
标准的双按钮,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];和程序里的顺序一样,cancel ok thirdButton 的buttonIndex 分别为0 1 2UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];
同理,cancel ok thirdButton FourthButton的buttonIndex 分别为0 1 2 3
[alertView show];
UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
可以通过if (buttonIndex == 1) { } 这样的来控制点击了某个按钮需要做什么操作
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)alertViewCancel:(UIAlertView *)alertView
alertViewCancel----》willDismissWithButtonIndex---》didDismissWithButtonIndex
- (BOOL)alertViewShouldEnableFirstOtherButton(UIAlertView *)alertView
设置yes / no 将会设置alertView 的第一个otherButton的enable属性
- (void)didPresentAlertView:(UIAlertView *)alertView
- (void)willPresentAlertView:(UIAlertView *)alertView
alertViewShouldEnableFirstOtherButton---->willPresentAlertView--->didPresentAlertView
---->clickedButtonAtIndex---->(如果会触发视图取消,则会调用alertViewCancel)willDismissWithButtonIndex---->didDismissWithButtonIndex
ios4.0以后 alertView不会自动随着程序转向后台而移除
UIAlertViewStyleDefault 只弹信息和按钮
UIAlertViewStyleSecureTextInput 有一个textfield加密框
UIAlertViewStylePlainTextInput 有一个不加密的textfield
UIAlertViewStyleLoginAndPasswordInput 有两个textfield,Login和password
只要有textfield就可以用textfieldAtIndex来捕获并进行相应的操作例如换键盘类型
开始是0,如果没有设置cancel button 则是-1
从0开始,如果没设置则是-1,而且没被设置则会被忽略
- (NSInteger)addButtonWithTitle:(NSString *)title
- (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, ...
- (UITextField *)textfieldAtIndex:(NSInteger)textfieldIndex
UIAlertViewStyleDefault 没有
UIAlertViewStyleSecureInput textfieldIndex 只有一个为0
UIAlertViewStylePlainInput textfieldIndex 只有一个为0
UIAlertViewStyleLoginAndPasswordInput textfieldIndex有两个 0 1