對(duì)UIAlertView& UIAlertController& UIActionSheet的一些整理

這幾天在做推送消息的處理,要有個(gè)提示框.花了幾個(gè)小時(shí),我整理了下UIAlertView與UIAlertController,閑話不多說,上代碼.

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  [self alertStyleWithTwoTextField];
  [self alertStyleWithTextField];
  [self alertStyle];
  [self actionSheetStyle];
  [self loginAndPassword];
  [self secureText];
  [self plainText];
  [self defaultAlert];
  [self actionSheet];

}

下面是幾種狀態(tài)

//原來的最
- (void)defaultAlert
{
    // iOS8被廢棄
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alertView" message:@"默認(rèn)樣式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看",@"評(píng)論", nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
}

當(dāng)你要使用提示框的按鈕,比如說跳轉(zhuǎn)的時(shí)候這時(shí)候你就要用到代理了

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"點(diǎn)擊的%ld",buttonIndex);
    if (buttonIndex == 1) {
        
        NSLog(@"點(diǎn)擊了這個(gè)是有效果的");
                secondViewController *secondVC = [[secondViewController alloc] init];
        
                [self presentViewController:secondVC animated:YES completion:nil];
    }

}

要說的一點(diǎn)是,這個(gè)提示框下方的按鈕你可以給他設(shè)置tag值,如果不設(shè)置的話那么從下往下數(shù)buttonIndex的值從1開始.最后的取消的buttonIndex值是0.

然后說說其他的

// 帶有明文輸入框
- (void)plainText
{
    // iOS8被廢棄
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"明文" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}

// 帶有密文輸入框
- (void)secureText
{
    // iOS8被廢棄
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"密文" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
    alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
    [alert show];
}

// 帶有登錄和密碼輸入框
- (void)loginAndPassword
{
    // iOS8被廢棄
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"登錄" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登錄",@"注冊(cè)", nil];
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    [alert show];
}

- (void)actionSheet
{
    // iOS8被廢棄
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"查看",@"評(píng)論", nil];
    [sheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld",buttonIndex);
    
}

可以復(fù)制過去模擬機(jī)看看.具體的也沒有什么好詳細(xì)說的.這個(gè)比較基礎(chǔ)了.以上都是比較老的了 ,下面說說iOS9之后的提示框

- (void)actionSheetStyle {
    
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"actionSheetStyle" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        
    }];
    UIAlertAction *pickAction = [UIAlertAction actionWithTitle:@"評(píng)論" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [actionSheetController addAction:pickAction];
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:showAllInfoAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

- (void)alertStyle
{
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"新版的alertView" message:@"這個(gè)在哪里的" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *pickAction = [UIAlertAction actionWithTitle:@"評(píng)論" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        secondViewController *secondVC = [[secondViewController alloc] init];
        
        [self presentViewController:secondVC animated:YES completion:nil];

        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [actionSheetController addAction:pickAction];
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:showAllInfoAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

- (void)alertStyleWithTextField
{
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"輸入姓名" preferredStyle:UIAlertControllerStyleAlert];
    [actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"請(qǐng)輸入姓名";
    }];
    
    UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [actionSheetController addAction:determineAction];
    [actionSheetController addAction:cancelAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}


- (void)alertStyleWithTwoTextField
{
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"登錄" preferredStyle:UIAlertControllerStyleAlert];
    [actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"賬號(hào)";
    }];
    [actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"密碼";
        textField.secureTextEntry = YES;
    }];
    
    UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [actionSheetController addAction:determineAction];
    [actionSheetController addAction:cancelAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

根據(jù)實(shí)際的使用對(duì)我來說是個(gè)更加的方便了,因?yàn)楫吘股倭撕芏嗟拇?你想要的按鈕動(dòng)作可以直接添加到block塊里面,一個(gè)對(duì)一個(gè)不容易混亂.

另外感謝@VV木公子(簡(jiǎn)書作者)的文章.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市址儒,隨后出現(xiàn)的幾起案子脾还,更是在濱河造成了極大的恐慌弯洗,老刑警劉巖新锈,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件含末,死亡現(xiàn)場(chǎng)離奇詭異晤愧,居然都是意外死亡泳梆,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門烈炭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來溶锭,“玉大人,你說我怎么就攤上這事符隙∨客保” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵霹疫,是天一觀的道長(zhǎng)拱绑。 經(jīng)常有香客問我,道長(zhǎng)丽蝎,這世上最難降的妖魔是什么猎拨? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮屠阻,結(jié)果婚禮上红省,老公的妹妹穿的比我還像新娘。我一直安慰自己国觉,他們只是感情好吧恃,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蛉加,像睡著了一般蚜枢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上针饥,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音需频,去河邊找鬼丁眼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛昭殉,可吹牛的內(nèi)容都是我干的苞七。 我是一名探鬼主播藐守,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼蹂风!你這毒婦竟也來了卢厂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤惠啄,失蹤者是張志新(化名)和其女友劉穎慎恒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體撵渡,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡融柬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了趋距。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片粒氧。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖节腐,靈堂內(nèi)的尸體忽然破棺而出外盯,到底是詐尸還是另有隱情,我是刑警寧澤翼雀,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布门怪,位于F島的核電站,受9級(jí)特大地震影響锅纺,放射性物質(zhì)發(fā)生泄漏掷空。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一囤锉、第九天 我趴在偏房一處隱蔽的房頂上張望坦弟。 院中可真熱鬧,春花似錦官地、人聲如沸酿傍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赤炒。三九已至,卻和暖如春亏较,著一層夾襖步出監(jiān)牢的瞬間莺褒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工雪情, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留遵岩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像尘执,于是被迫代替她去往敵國(guó)和親舍哄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容