UIAlertView已弃用如何将此代码修复为UIAlertController?我无法弄清楚如何在UIAlertController中使用switch语句

时间:2016-07-22 07:45:14

标签: objective-c ios9 uialertcontroller xcode7.3

我已经尝试了几种方法来修复这个已弃用的代码,但没有任何帮助我,我对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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

-(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];
}

答案 1 :(得分:1)

  

在iOS 8中对AlertView进行了描述。因此我们需要使用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];