對UIAlertView& UIAlertController& UIActionSheet的一些整理

這幾天在做推送消息的處理,要有個提示框.花了幾個小時,我整理了下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:@"默認樣式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看",@"評論", nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
}

當你要使用提示框的按鈕,比如說跳轉的時候這時候你就要用到代理了

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

}

要說的一點是,這個提示框下方的按鈕你可以給他設置tag值,如果不設置的話那么從下往下數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:@"登錄",@"注冊", nil];
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    [alert show];
}

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

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

可以復制過去模擬機看看.具體的也沒有什么好詳細說的.這個比較基礎了.以上都是比較老的了 ,下面說說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:@"評論" 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:@"這個在哪里的" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *pickAction = [UIAlertAction actionWithTitle:@"評論" 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 = @"請輸入姓名";
    }];
    
    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 = @"賬號";
    }];
    [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];
}

根據實際的使用對我來說是個更加的方便了,因為畢竟少了很多的代理,你想要的按鈕動作可以直接添加到block塊里面,一個對一個不容易混亂.

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容