警告框,當警告框彈出來後,用戶沒法與界面內的其餘控件進行交互。 UIAlertView 在view的中間彈出警告框。UIAlertView 的建立很是簡單,而後顯示出來就能夠了。 #import "ViewController.h" @implementation ViewController @synthesize btn; //視圖的加載 - (void)viewDidLoad { [super viewDidLoad]; btn = [[UIButton alloc] initWithFrame:CGRectMake(100,100 ,200, 30)]; [btn setTitle:@"Alter" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //建立Button,在Button的點擊事件中顯示AlertView [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //添加到視圖中去 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //按鈕的點擊事件 -(void)click:(UIButton *)sender{ //建立UIViewAlert UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"我出現了" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil]; //定製彈出框的樣式 /* UIAlertViewStyleDefault //默認 UIAlertViewStylePlainTextInput //帶輸入框 UIAlertViewStyleSecureTextInput//帶密碼輸入框 UIAlertViewStyleLoginAndPasswordInput //登錄輸入框 */ alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert textFieldAtIndex:1].keyboardType = UIKeyboardTypeNumberPad; //設定鍵盤的類型 [alert show]; //alertview 顯示出來 } //視圖控制器要實現<UIAlertViewDelegate>,重寫下面的方法,來實現AlertView的委託 //當用戶點擊警告框中的某個按鈕的時候激活該方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // NSString *msg = [NSString stringWithFormat:@"你點擊了第%ld個按鈕",buttonIndex]; // UIAlertView *altert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles: nil]; if (buttonIndex == 1) { UITextField *nameField = [alertView textFieldAtIndex:0]; UITextField *passField = [alertView textFieldAtIndex:1]; //顯示用戶輸入的用戶名和密碼 NSString *msg = [NSString stringWithFormat:@"你輸入的用戶名爲:%@,密碼爲:%@", nameField.text,passField.text]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"信息" message:msg delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil]; [alert show]; } } //這個也是AlertViewDelegate中的方法,當警告框將要顯示的時候會調用該方法, //用戶能夠童工該方法,來定製UIAlertView - (void)willPresentAlertView:(UIAlertView *)alertView{ //提示框將要顯示出來的時候 //便利alertview 下面全部的子控件 for (UIView *vi in alertView.subviews) { NSLog(@"%@",vi.class); if ([vi isKindOfClass:[UILabel class]]) { //若是是label控件 UILabel *label = (UILabel *)vi; //將label的文字設定爲左對齊 label.textAlignment = NSTextAlignmentRight; } } } @end UIActionSheet 和UIAlertView 差很少,UIActionSheet表現爲顯示在屏幕底部的按鈕序列,用戶經過點擊某個按鈕來表 明本身的態度。UIActionSheet 只支持一個標題和多個按鈕,會有兩個固定按鈕。 #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize btn; - (void)viewDidLoad { [super viewDidLoad]; btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(100, 100,100,20); [btn setTitle:@"肯定" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; //點擊按鈕調用顯示UIActionSheet的方法 [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //點擊時間,建立actionsheet -(void)btnClick:(UIButton *) btn{ //初始化一個UIActionSheet UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"銷燬" otherButtonTitles:@"btn1",@"btn2",@"btn3", nil]; /* UIActionSheetStyleDefault 默認 UIActionSheetStyleBlackOpaque 黑背景白文字 UIActionSheetStyleBlackTranslucent 透明黑色背景白文字 */ sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; [sheet showInView:self.view]; } //協議中的方法,點擊各個btn的時候觸發 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSString *msg = [NSString stringWithFormat:@"你點擊了第%ld個按鈕",buttonIndex]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [alert show]; //顯示UIActionSheet } @end