在iOS8之前彈窗一共有兩種方式分爲UIAlertView和UIActionSheet,在iOS8之後新增UIAlertViewController來統一管理,下面就來一一作介紹
1.UIAlertView
// message 可設置爲nil,cancelButtonTitle也可設置爲nil,otherButtonTitles可設置多個button
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"溫馨提示" message:@"你要選擇點擊哪一個" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定"];
[alert show];
[alert release]
//代理方法
-(void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLong(@”你點擊的是第%d個”, buttonIndex);
}
2.UIActionSheet
UIActionSheet *actionSheet =[[UIActionSheet alloc] initWithTitle:@"溫馨提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:@"待定"];
[actionSheet showInView:self.view];
[actionSheet release];
//代理方法
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"點擊了第%d個", buttonIndex);
if (buttonIndex == actionSheet.cancelButtonIndex) {
return;
}
switch (buttonIndex) {
case 0: {
NSLog(@"你點擊了確定");
break;
}
case 1: {
NSLog(@"你點擊了待定");
break;
}
}
}
3.UIAlertController
// preferredStyle設置彈窗樣式,本文用的是UIAlertControllerStyleAlert,具體可根據實際要求選擇
UIAlertController *alertcontroller =[UIAlertController alertControllerWithTitle:@"溫馨提示"message:@"你已經進入警告區域" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction*action = [UIAlertAction actionWithTitle:@"確定"style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)) {
NSLog(@"你點擊了確定");
}];
UIAlertAction *action1 = [UIAlertActionactionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"你點擊了取消");
}];
[alertcontroller addAction:action];
[alertcontroller addAction:action1];
[self presentViewController:alertcontroller animated:YES completion:nil];