提示框是做項(xiàng)目時候經(jīng)常遇到的一個功能牺氨,可以提高用戶體驗(yàn)须妻,除了一些著名的第三方庫以外埂伦,我們更多會用到系統(tǒng)提供的提示框和上拉菜單煞额。這里總結(jié)UIAlertView、UIActionSheet沾谜、UIAlertController的一些基礎(chǔ)用法膊毁。
iOS8之前用的是UIAlertView、UIActionSheet基跑,在iOS8之后UIAlertController就取代了前面兩個婚温。
UIAlertView
這個方法通過設(shè)置一個標(biāo)題,內(nèi)容和些按鈕創(chuàng)建提示框媳否,代碼示例如下:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網(wǎng)絡(luò)請求失敗" message:@"請檢查網(wǎng)絡(luò)設(shè)置" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:@"第一項(xiàng)",@"第二項(xiàng)", nil];
//設(shè)置樣式
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
UIAlertController
UIAlertController包含了UIActionSheet栅螟, UIAlertView的功能
提示框的功能,代碼示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標(biāo)題" message:@"這個是UIAlertController的默認(rèn)樣式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
上拉菜單的功能篱竭,代碼示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"刪除數(shù)據(jù)將不可恢復(fù)" preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
[self presentViewController:alertController animated:YES completion:nil];
如果對你有幫助力图,那就點(diǎn)個贊吧~