UIAlertView的使用介绍
- (void)alert {
alert=[[UIAlertView alloc]initWithTitle:@”Message” message:@”Are you OK?” delegate:self cancelButtonTitle:@”cancel” otherButtonTitles:@”Yes”, nil];
alert.delegate=self;
//title可以更新alert的标题
alert.title=@”heheheda”;
//numerOfButtons只读属性,返回alert的按钮总数
NSLog(@”alert有%ld个button”,(long)alert.numberOfButtons);
//cancelbuttonIndex获得取消按钮的index,
NSLog(@”alert的取消button是%ld”,(long)alert.cancelButtonIndex);
NSLog(@”alert的第一个button是%ld”,(long)alert.firstOtherButtonIndex);
//alertView的style,必须设置alert的代理
//alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
NSLog(@”%@”,[alert textFieldAtIndex:0]);
[alert show];
}
pragma mark UIAlertView Delegate
//delegater方法的调用顺序:willPresentAlertView–>alertViewShouldEnableFirstOtherButton–>didPresentAlertView–>clickedButtonAtIndex–>alertView:willDismissWithButtonIndex–>alertrView:didDismissWithButtonIndex
//获取用户点击的按钮,并做响应的处理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//buttonTitleAtindex:根据给的buttonIndex返回按钮的标题
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@”Yes”]) {
NSLog(@”点击了Yes按钮”);
//接受输入类容
//textFieldAtIndex 获取对应位置的UITextField
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(@”%@”,textField.text);
}
}
//当用户按下HOME键的时候,回调此方法,用户点击Cancel按钮的时候不会回调此方法
- (void)alertViewCancel:(UIAlertView *)alertView{
NSLog(@”点击了alertView的取消按钮”);
}
(void)willPresentAlertView:(UIAlertView *)alertView {
NSLog(@”alertView即将显示”);
}(void)didPresentAlertView:(UIAlertView *)alertView{
NSLog(@”alertView已经显示”);
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:
(NSInteger)buttonIndex {
NSLog(@”alertView将消失”);
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@”alertView已经消失”);
}
//一直在调用,设置alert中的A按钮禁用,如果满足条件,A按钮可用
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
UITextField *textField = [alertView textFieldAtIndex:0];
if (!textField.text || [textField.text isEqualToString:@”“])
{
NSLog(@”NO”);
return NO;
} else {
NSLog(@”yes”);
return YES;
}
}