IOS中的三種彈窗模式,IOS三種彈窗模式

來源:互聯網
上載者:User

IOS中的三種彈窗模式,IOS三種彈窗模式

#pragma mark 方法1

/**

 *  用在IOS7,用到了代理

 */

- (void)use1

{

    // 1.建立一個中間彈框,有“取消”和“確定按鈕”,設定代理為當前控制器,由控制器監聽點擊了“取消”還是“確定”按鈕

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"點擊了圖片按鈕" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];

    

    // 2.顯示在螢幕上

    [alert show];

}

#pragma mark 監聽方式1中出現的彈框中的按鈕點擊,控制器來監聽點擊了取消還是確定按鈕

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    // 預設取消按鈕索引為0

    if (buttonIndex == 0) NSLog(@"點擊了取消按鈕");

    else NSLog(@"點擊了確定按鈕");

}

 

#pragma mark 方法2

/**

 *  用在IOS8,沒有代理。點擊按鈕時要執行的操作放在了block中,因此不需要設定代理

 */

- (void)use2

{

    // 1.建立彈框控制器, UIAlertControllerStyleAlert這個樣式代表彈框顯示在螢幕中央

    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"點擊了頭像" preferredStyle:UIAlertControllerStyleAlert];

 

    // 2.添加取消按鈕,block中存放點擊了“取消”按鈕要執行的操作

   UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"點擊了取消按鈕");

    }];

    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSLog(@"點擊了確定按鈕");

    }];

    // 3.將“取消”和“確定”按鈕加入到彈框控制器中

    [alertVc addAction:cancle];

    [alertVc addAction:confirm];

    

    // 4.控制器 展示彈框控制項,完成時不做操作

    [self presentViewController:alertVc animated:YES completion:^{

        nil;

    }];

}

 

#pragma mark 方法3

/**

 *  用在IOS8,沒有用到代理。跟方式2唯一不同的是:彈框的樣式變為“UIAlertControllerStyleActionSheet”, 彈框出現在螢幕底部

 */

- (void)use3

{

    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"點擊了頭像" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"點擊了取消");

    }];

    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSLog(@"點擊了確定按鈕");

    }];

    [alertVc addAction:cancle];

    [alertVc addAction:confirm];

    

    [self presentViewController:alertVc animated:YES completion:^{

        nil;

    }];

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.