這幾天在做推送消息的處理,要有個(gè)提示框.花了幾個(gè)小時(shí),我整理了下UIAlertView與UIAlertController,閑話不多說,上代碼.
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self alertStyleWithTwoTextField];
[self alertStyleWithTextField];
[self alertStyle];
[self actionSheetStyle];
[self loginAndPassword];
[self secureText];
[self plainText];
[self defaultAlert];
[self actionSheet];
}
下面是幾種狀態(tài)
//原來的最
- (void)defaultAlert
{
// iOS8被廢棄
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alertView" message:@"默認(rèn)樣式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看",@"評(píng)論", nil];
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];
}
當(dāng)你要使用提示框的按鈕,比如說跳轉(zhuǎn)的時(shí)候這時(shí)候你就要用到代理了
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"點(diǎn)擊的%ld",buttonIndex);
if (buttonIndex == 1) {
NSLog(@"點(diǎn)擊了這個(gè)是有效果的");
secondViewController *secondVC = [[secondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
}
}
要說的一點(diǎn)是,這個(gè)提示框下方的按鈕你可以給他設(shè)置tag值,如果不設(shè)置的話那么從下往下數(shù)buttonIndex的值從1開始.最后的取消的buttonIndex值是0.
然后說說其他的
// 帶有明文輸入框
- (void)plainText
{
// iOS8被廢棄
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"明文" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
// 帶有密文輸入框
- (void)secureText
{
// iOS8被廢棄
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"密文" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[alert show];
}
// 帶有登錄和密碼輸入框
- (void)loginAndPassword
{
// iOS8被廢棄
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"登錄" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登錄",@"注冊(cè)", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
- (void)actionSheet
{
// iOS8被廢棄
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"查看",@"評(píng)論", nil];
[sheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld",buttonIndex);
}
可以復(fù)制過去模擬機(jī)看看.具體的也沒有什么好詳細(xì)說的.這個(gè)比較基礎(chǔ)了.以上都是比較老的了 ,下面說說iOS9之后的提示框
- (void)actionSheetStyle {
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"actionSheetStyle" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *pickAction = [UIAlertAction actionWithTitle:@"評(píng)論" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:pickAction];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:showAllInfoAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)alertStyle
{
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"新版的alertView" message:@"這個(gè)在哪里的" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *pickAction = [UIAlertAction actionWithTitle:@"評(píng)論" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
secondViewController *secondVC = [[secondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:pickAction];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:showAllInfoAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)alertStyleWithTextField
{
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"輸入姓名" preferredStyle:UIAlertControllerStyleAlert];
[actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請(qǐng)輸入姓名";
}];
UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:determineAction];
[actionSheetController addAction:cancelAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)alertStyleWithTwoTextField
{
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"登錄" preferredStyle:UIAlertControllerStyleAlert];
[actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"賬號(hào)";
}];
[actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密碼";
textField.secureTextEntry = YES;
}];
UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:determineAction];
[actionSheetController addAction:cancelAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
根據(jù)實(shí)際的使用對(duì)我來說是個(gè)更加的方便了,因?yàn)楫吘股倭撕芏嗟拇?你想要的按鈕動(dòng)作可以直接添加到block塊里面,一個(gè)對(duì)一個(gè)不容易混亂.
另外感謝@VV木公子(簡(jiǎn)書作者)的文章.