在iOS8之前,alert通常是用UIAlertView来实现的,但是在iOS8以后,如果当前的界面上有输入框(UITextView,UITextField)时,当点击UIAlertView进行跳转,特别是pop到上一个页面时,会出现在跳转以后键盘并没有收起来,而是闪一下再消失的问题。这是由于UIAlertView在弹出的时候会使当前界面失去焦点,同时会打断键盘收起的动画,所以出现了这样的情况。 在iOS8的SDK中,苹果提倡使用UIAlertController来取代UIAlertView。
废话不说,直接上代码:
// '先来定义个判断当前版本的宏'
#define SYSTEM_VERSION [[UIDevice currentDevice].systemVersion floatValue]
// '以下是如何实现UIAlertController的代码'
if (SYSTEM_VERSION >= 8.0) {
UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"警告标题" message:@"警告内容" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.navigationController popViewControllerAnimated:YES];
}];
[alertCtrl addAction:okAction
大专栏
iOS8 UIAlertView导致键盘闪退的问题an class="p">];
//
'这里可以添加多个action,每个action单独写需要进行的操作'
[
self
presentViewController
:alertCtrl
animated
:YES
completion
:nil
];
}
else
{
//
'这个else一定要写,否则会导致在ios8以下的真机crash'
UIAlertView
*
alert
=
[[
UIAlertView
alloc
]
initWithTitle
:
@
"赎回成功"
message
:nil
delegate
:self
cancelButtonTitle
:nil
otherButtonTitles
:
@
"确定"
,
nil
];
alert
.
tag
=
998
;
[
alert
show
];
}
#pragma mark-UIAlertViewDelegate
-
(
void
)
alertView
:(
UIAlertView
*
)
alertView
clickedButtonAtIndex
:(
NSInteger
)
buttonIndex
{
if
(
alertView
.
tag
==
998
)
{
[
self
.
navigationController
popViewControllerAnimated
:YES
];
}
}
深呼吸,command + C/V,改一下内容,收工~