iOS9彈框的最新兩種方式(解決控制器以外的類無法彈出的問題)


1、彈框出現在屏幕中間位置

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出" preferredStyle: UIAlertControllerStyleAlert]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //點擊確認后需要做的事
    }]];
    [self presentViewController:alert animated:YES completion:nil]; //注意一定要寫此句,否則不會顯示

 

此方法可以添加文本框,輸入內容

 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"請輸入名字";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"請輸入價格";
    }];

 

2、彈框出現在屏幕底部(兩種方式的不同點在於代碼第一行最后的,底部是UIAlertControllerStyleActionSheet

1 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出" preferredStyle: UIAlertControllerStyleActionSheet]; 
2     [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
3     [alert addAction:[UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
4         //點擊確認后需要做的事
5     }]];
6     [self presentViewController:alert animated:YES completion:nil]; //注意一定要寫此句
7  
[self showViewController:alert sender:nil]; //此句也可以
 
        

 注:如果是其它類,不是控制器,則可以用下面方法讓彈框顯現出來:

1 UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
2     [vc showViewController:alert sender:nil];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM