iOS8之后,Xcode提供的提醒框架啟用UIAlertController,用法跟之前差別還有的,見效果圖和代碼.
demo演示
1.樣式一:
self.alertVc = [UIAlertController alertControllerWithTitle:@"標(biāo)題一" message:@"這里是要顯示的信息" preferredStyle:UIAlertControllerStyleAlert];
//彈出視圖,使用UIViewController的方法
[self presentViewController:self.alertVc animated:YES completion:^{
//隔一會就消失
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
2.樣式二:
self.alertVc = [UIAlertController alertControllerWithTitle:@"標(biāo)題二" message:@"這里是要顯示的信息" preferredStyle:UIAlertControllerStyleActionSheet];
//彈出視圖,使用UIViewController的方法
[self presentViewController:self.alertVc animated:YES completion:^{
//隔一會就消失
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
3.樣式三:
self.alertVc = [UIAlertController alertControllerWithTitle:@"標(biāo)題三" message:@"這是標(biāo)題內(nèi)容" preferredStyle:UIAlertControllerStyleActionSheet];//UIAlertControllerStyleActionSheet
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
#warning 修改字體顏色,屬于私有Api 慎用
[cancelAction setValue:[UIColor redColor] forKey:@"_titleTextColor"];[self.alertVc addAction:cancelAction];
[self.alertVc addAction:sureAction];
//彈出視圖,使用UIViewController的方法
[self presentViewController:self.alertVc animated:YES completion:^{
}];
4.樣式四:
self.alertVc = [UIAlertController alertControllerWithTitle:@"標(biāo)題四" message:@"標(biāo)題內(nèi)容" preferredStyle:UIAlertControllerStyleAlert];
[self.alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入用戶名";
}];
[self.alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入密碼";
textField.secureTextEntry = YES;
}];
//對UITextField開始監(jiān)聽
[self.alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"輸入可以監(jiān)聽到";
_textField = textField;
#warning 監(jiān)聽輸入,可以使用通知,可以kvo等,如果是下邊的這種方式,容易retain,造成循環(huán)引用,所以聲明一個(gè)全局變量textField,然后在block監(jiān)聽,就不會造成循環(huán)引用(capturing 'self'strong in this block is likely to lead to a retain cycle)
// [textField addTarget:self action:@selector(watchTextFieldMethod:) forControlEvents:UIControlEventEditingChanged];
}];
[_textField addTarget:self action:@selector(watchTextFieldMethod:) forControlEvents:UIControlEventEditingChanged];
//添加確定和取消按鈕
UIAlertAction *cacleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
[self.alertVc addAction:cacleAction];
[self.alertVc addAction:sureAction];
[self presentViewController:self.alertVc animated:YES completion:^{
}];