Search code examples
objective-cios9uialertcontrollerxcode7.3

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


I've tried Several ways to fix this deprecated code but nothing helped me I'm new to Objective C and iOS please help me fix this...It's working fine in iOS8 but not in 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;
        }
    }
}

Solution

  • 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];