Objective-C - UIAlertView 不委托

Objective-C - UIAlertView doesn't delegate

我有这段代码,我正在尝试在警报视图上获取语言选择器,但我认为委托不起作用。

@interface JumboEntranceViewController () <FBSDKLoginButtonDelegate, WSLoginDelegate, UIAlertViewDelegate, UITextFieldDelegate>
...
...
...
...

- (void) showLanguageSelector {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Idioma" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"English",@"Español",@"Deustch", nil];
    alert.delegate = self;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0) { //CANCEL
        [[LanguagesManager sharedInstance] setLanguage:@"es"];
    }
    else if(buttonIndex == 1) {
        [[LanguagesManager sharedInstance] setLanguage:@"en"];
    }
    else if(buttonIndex == 2) {
        [[LanguagesManager sharedInstance] setLanguage:@"es"];
    }
    else if (buttonIndex == 3) {
        [[LanguagesManager sharedInstance] setLanguage:@"de"];
    }
}

但是我无法进入函数 didDismissWithButtonIndex

首先,正如评论中提到的,UIAlertView 自 iOS 9.0 以来已被弃用。您应该改用 UIAlertController,就像 Xcode 在 UIAlertView 和委托方法

的实现中告诉您警告一样

无论如何,我实现了您代码的精确副本并且它有效:您确定问题不在 LanguageManager 单例中吗?您是否尝试在 alertview:didDismissWithButtonIndex: 的第一行放置断点以查看它是否被调用?

[edit] 为了澄清,下面是代码,and here's the proof it works:

#import "ViewController.h"

@interface ViewController ()<UIAlertViewDelegate>
@end

@implementation ViewController

- (void)viewDidLoad {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Idioma"
                                                    message:@"" 
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"English",@"Español",@"Deustch", nil];
    alert.delegate = self;
    [alert show];
}


-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0) { //CANCEL
        NSLog(@"0");
    }
    else if(buttonIndex == 1) {
        NSLog(@"1");
    }
    else if(buttonIndex == 2) {
        NSLog(@"2");
    }
    else if (buttonIndex == 3) {
        NSLog(@"3");
    }
}

@end

使用此代码。 (自 iOS 9.0 起不推荐使用 UIAlertView。)

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

    [alert addAction:[UIAlertAction actionWithTitle:@"English" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[LanguagesManager sharedInstance] setLanguage:@"en"];
    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"Español" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[LanguagesManager sharedInstance] setLanguage:@"es"];
    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"Deustch" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[LanguagesManager sharedInstance] setLanguage:@"de"];
    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [[LanguagesManager sharedInstance] setLanguage:@"es"];
    }]];

    [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:alert animated:YES completion:nil];