一,UIAlertView警告框
(1) UIAlertView * alert = [[UIAlertViewalloc]
initWithTitle:@"Notice"message:@"the alert"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:@"Options1",@"Options2",nil];
[alert show];
很简单,上述方法可以产生并显示一个警告框。警告框上的button的内容就是设置ButtonTitle。关键是警告框给提供的协议方法。(2)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
这个方法是当用户点击警告框中的某个按键时会触发该方法,buttonIndex代表按钮的索引,从0开始
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法是完全隐藏警示框出来之后会触发
- (void)alertViewCancel:(UIAlertView *)alertView
这个是在警告框被取消时触发
- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
这个方法是在将要显示隐藏的时候触发- (void)willPresentAlertView:(UIAlertView *)alertView
这个是警告框将要显示出来的时候触发-(void) didPresentAlertView:(UIAlertView *)alertView
这个是警告框完全显示出来的时候触发(3)该控件有个属性,actionSheetStyle,用来设置风格,比如可以使其带有一个输入框,带有密码输入框等,举个栗子,输入用户名和密码
- (IBAction)clicked:(id)sender {
UIAlertView * alert = [[UIAlertViewalloc]
initWithTitle:@"Log on"message:@"please input the user name and the password"delegate:selfcancelButtonTitle:@"Cancle"otherButtonTitles:@"OK",nil];
alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;
[alert textFieldAtIndex:1].keyboardType =UIKeyboardTypeNumberPad;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
UITextField * nameField = [alertViewtextFieldAtIndex:0];
UITextField * passField = [alertViewtextFieldAtIndex:1];
NSString * msg = [NSStringstringWithFormat:@"you have input the user name : %@ and the password : %@",nameField.text,passField.text];
UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:@"notice"message:msg delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];
[alert show];
}
}
- (void) willPresentAlertView:(UIAlertView *)alertView{
for (UIView * viewin alertView.subviews) {
if ([view isKindOfClass:[UILabelclass]]) {
UILabel * lable = (UILabel *)view;
lable.textAlignment =UITextAlignmentLeft;
}
}
}
二,UIActionSheet
这个控件跟Alert非常之像,写一点简单的使用就可以知道了
- (IBAction)clicked:(id)sender {
UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"make sure to delete" delegate:self cancelButtonTitle:@"Cancle" destructiveButtonTitle:@"OK" otherButtonTitles:@"Options1", @"Options2",nil];
sheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[sheet showInView:self.view];
}
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString * msg = [NSString stringWithFormat:@"you have clicked the %ld button",(long)buttonIndex];
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"notice" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}