UIAlertController的使用及在AppDelegate.m中添加不上的解決辦法


  今天在xcode8.1的AppDelegate.m中要添加一個提示框,發現添加不上,試了試UIAlertView可以正常添加,但是在xcode8.0之后UIAlertView就被UIAlertController取代了,所以嘛,咱們也要與時俱進嘛!

 一、UIAlertController的簡單使用:

//創建UIAlertController
    //UIAlertControllerStyle 這是提示框顯示的兩種形式
    //UIAlertControllerStyleAlert 在屏幕中央顯示
    //UIAlertControllerStyleActionSheet 從屏幕底部彈出
    UIAlertController *alertCtl = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示框" preferredStyle:UIAlertControllerStyleAlert];
    //提示框的響應動作  UIAlertAction
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"actionOne" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //點擊事件可以在這里添加
        NSLog(@"點擊了ActionOne");
    }];
    UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"actionTwo" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *actionThree = [UIAlertAction actionWithTitle:@"actionThree" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"點擊了actionThree");
    }];
//添加動作至UIAlertController控制器上 [alertCtl addAction:actionOne]; [alertCtl addAction:actionTwo]; [alertCtl addAction:actionThree];
[self presentViewController:alertCtl animated:YES completion:nil];

二、在AppDelegate中的使用:

    //初始化UIAlertController
    UIAlertController *alertCtl = [UIAlertController alertControllerWithTitle:@"提示" message:@"AppDelegate中" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertCtl addAction:alertAction];
    //初始化UIWindows
    UIWindow *aW = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    aW.rootViewController = [[UIViewController alloc]init];
    aW.windowLevel = UIWindowLevelAlert + 1;
    [aW makeKeyAndVisible];
    [aW.rootViewController presentViewController:alertCtl animated:YES completion:nil];

 


免責聲明!

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



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