UIAlertView 已弃用 我如何将此代码修复为 UIAlertController?我不知道如何在 UIAlertController 中使用 switch 语句

UIAlertView is Deprecated how can i fix this code as UIAlertController? I can't figure out how to use switch statement in UIAlertController

我已经尝试了多种方法来修复这个已弃用的代码,但没有任何帮助我是 Objective C 和 iOS 的新手,请帮助我修复这个问题...它在 iOS8 但不在 iOS9..

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [super alertView:alertView clickedButtonAtIndex:buttonIndex];

    if (buttonIndex)
    {
        switch (alertView.tag)
        {
            case kAccessAddressBook:
            {
                [self displayFindFriendView:[NSNumber numberWithInteger: CS_CONTACTS ]];
            }
                break;
            case kFindFriendEmail:
            {

            }
                break;
            case kLogout:
            {
                // Hit Logout API
                [self userLogout];
            }
                break;
            case kClearSearchHistory:
            {
                // Clear Search History Data base.
                [[CSCoreDataHandler sharedInstance] deleteManagedObjectsInModel:@"CSRecentSearch"];

            }
                break;
            default:
                break;
        }
    }
}
-(void)alert
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                                   message:@"This is an alert."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    if (buttonIndex)
    {
        switch (alertView.tag)
        {
            case kAccessAddressBook:
            {


                defaultAction = [UIAlertAction actionWithTitle:[self displayFindFriendView:[NSNumber numberWithInteger: CS_CONTACTS ]]; style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {}];
            }
                break;
            case kFindFriendEmail:
            {

            }
                break;
            case kLogout:
            {
                // Hit Logout API
                [self userLogout];
            }
                break;
            case kClearSearchHistory:
            {
                // Clear Search History Data base.
                [[CSCoreDataHandler sharedInstance] deleteManagedObjectsInModel:@"CSRecentSearch"];

                defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {}];

            }
                break;
            default:
                break;
        }
    }



    [alert addAction:defaultAction];

    [self presentViewController:alert animated:YES completion:nil];
}

AlertView is depricated in iOS 8.So we need to use UIAlertController.

UIAlertController * alert = [UIAlertController
            alertControllerWithTitle:@"Title"
                             message:@"Message"
                      preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *actionAccessAddressbook = [UIAlertAction
                    actionWithTitle:@"Access"
                              style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action) {
                         [self displayFindFriendView:[NSNumber numberWithInteger: CS_CONTACTS ]];
                            }];

UIAlertAction *actionFindFriendEmail = [UIAlertAction
                        actionWithTitle:@"Find Friend Email"
                                  style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                   //...Do your stuff here
                                }];

UIAlertAction *actionLogout = [UIAlertAction
                        actionWithTitle:@"Logout"
                                  style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                   [self userLogout];
                                }];
UIAlertAction *actionClearSearchHistory = [UIAlertAction
                        actionWithTitle:@"ClearSearchHistory"
                                  style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                  [[CSCoreDataHandler sharedInstance] deleteManagedObjectsInModel:@"CSRecentSearch"];
                                }];


[alert addAction:actionAccessAddressbook];
[alert addAction:actionFindFriendEmail];
[alert addAction:actionLogout];
[alert addAction:actionClearSearchHistory];

[self presentViewController:alert animated:YES completion:nil];