在iOS開發(fā)中常常需要實(shí)現(xiàn)彈出選擇框,選擇框可以添加多項(xiàng).其主要功能是提醒用戶,向用戶展示提醒消息
效果圖
彈出選擇按鈕框.gif
這里介紹兩種實(shí)現(xiàn)方法:
第一種: UIActionSheet是一個(gè)非常有用的類锈死,我就在應(yīng)用中經(jīng)常用它
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"收藏",@"舉報(bào)", nil];
[sheet showInView:self];
參數(shù)說明:
title:視圖標(biāo)題
delegate:設(shè)置代理
cancelButtonTitle:取消按鈕的標(biāo)題
destructiveButtonTitle:特殊標(biāo)記的按鈕的標(biāo)題
otherButtonTitles:其它按鈕的標(biāo)題
第二種:利用UIAlertController
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊了取消");
}];
[alertVc addAction:action];
UIViewController *rootVc = [UIApplication sharedApplication ].keyWindow.rootViewController;
[rootVc presentViewController:alertVc animated:YES completion:nil];
用到這種方法時(shí)要注意一個(gè)點(diǎn),選擇框從底部彈出, 利用modal實(shí)現(xiàn), 而只有控制器才能modal,
那該如何在一個(gè)控件里面實(shí)現(xiàn)modal? 獲取窗口的根控制器可以實(shí)現(xiàn).