CountryPicker UIPickerView 调用子类方法而不是它自己的方法

CountryPicker UIPickerView calls subclassed method instead of its own

所以我在一个 ViewController 中使用 2 个 UIPickerView 控件时遇到了这个问题。我正在使用 CountryPicker 在这里 https://github.com/nicklockwood/CountryPicker 显示一个不错的国家选择器,这样用户就可以 select 他或她的国家,以及他或她的国家代码phone 号。我想限制其中一个选择器中的国家/地区数量,并在另一个选择器中显示所有国家/地区。但是当我显示 2 个选择器时,似乎具有 CountryPicker 类型的选择器实际上使用了 -countriesOfOperation 中的覆盖方法 [=29] =]CustomCountryPicker class.

我尝试使用故事板,然后以编程方式初始化 2 个选择器,但没有成功。如您所见,我尝试在显示第二个选择器之前删除第一个选择器,反之亦然,但它不会修复它。

即使我将 *countrySelectionPickerView 设置为 CountryPicker,它仍然使用来自 CustomCountryPicker?所以对象的类型是 CountryPicker,但它仍然显示简短的国家/地区列表。

CustomCountryPicker.h

#import <CountryPicker/CountryPicker.h>

@interface CustomCountryPicker : CountryPicker

/// Returns a dictionary of country names, keyed by country code.
+(NSDictionary<NSString *, NSString *> *)countryNamesByCode;

@end

CustomCountryPicker.m

#import "CustomCountryPicker.h"

@implementation CustomCountryPicker

-(instancetype)initWithFrame:(CGRect)frame
{
    return [super initWithFrame:frame];
}

+(NSDictionary *)countriesOfOperation
{
    NSDictionary *countries = @{ @"DK": @"Denmark",
                                 @"DE": @"Germany",
                                 @"GB": @"Great Britain",
                                 @"FR": @"France",
                                 @"NO": @"Norway",
                                 @"CH": @"Switzerland"
                                };
    return countries;
}

+(NSDictionary *)countryNamesByCode
{
    NSDictionary *countries = [self countriesOfOperation];
    static NSDictionary *_countryNamesByCode = nil;
    if (!_countryNamesByCode)
    {
        NSMutableDictionary *namesByCode = [NSMutableDictionary dictionary];
        for (NSString *code in [NSLocale ISOCountryCodes])
        {
            if([countries valueForKey:code]) {
                NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:code];

                //workaround for simulator bug
                if (!countryName)
                {
                    countryName = [[NSLocale localeWithLocaleIdentifier:@"en_US"] displayNameForKey:NSLocaleCountryCode value:code];
                }
                countryName = [countryName stringByAppendingString:@" XXX"];
                namesByCode[code] = countryName ?: code;
            }
        }
        _countryNamesByCode = [namesByCode copy];
    }
    return _countryNamesByCode;
}

SomeViewController.h

#import <CountryPicker/CountryPicker.h>
...

SomeViewController.m

#import "CustomCountryPicker.h"

@interface SomeViewController () <CountryPickerDelegate>

@property (nonatomic, weak) IBOutlet UIView *countryPickerView;
@property (nonatomic, weak) IBOutlet UIView *customCountryPickerView;
@property (nonatomic, strong) CountryPicker *countrySelectionPickerView;
@property (nonatomic, strong) CustomCountryPicker *customCountrySelectionPickerView;

-(IBAction)countrySelection:(id)sender {

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
UIButton *senderButton = (UIButton *)sender;

if(senderButton.tag == 101) {
    [UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{
        self.countryPickerView.hidden = TRUE;
        [self.countrySelectionPickerView removeFromSuperview];
        [self.customCountrySelectionPickerView removeFromSuperview];
        self.customCountrySelectionPickerView = [[CustomCountryPicker alloc] initWithFrame:CGRectMake(0, 44, 375, 216)];
        self.customCountrySelectionPickerView.tag = 11;
        self.customCountrySelectionPickerView.delegate = self;
        [self.customCountryPickerView addSubview:self.customCountrySelectionPickerView];
        self.customCountryPickerView.hidden = FALSE;
        self.customCountryPickerView.frame = CGRectMake(0, SCREEN_HEIGHT - 260, SCREEN_WIDTH, 260);
    } completion:nil];

    // update user country

// select user country phone code
} else if(senderButton.tag == 102) {
    [UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{
        self.customCountryPickerView.hidden = TRUE;
        [self.countrySelectionPickerView removeFromSuperview];
        [self.customCountrySelectionPickerView removeFromSuperview];
        self.countrySelectionPickerView = [[CountryPicker alloc] initWithFrame:CGRectMake(0, 44, 375, 216)];
        self.countrySelectionPickerView.tag = 12;
        self.countrySelectionPickerView.delegate = self;
        [self.countryPickerView addSubview:self.countrySelectionPickerView];
        self.countryPickerView.hidden = FALSE;
        self.countryPickerView.frame = CGRectMake(0, SCREEN_HEIGHT - 260, SCREEN_WIDTH, 260);
    } completion:nil];
}

这是因为库正在使用 static 变量,因此您的自定义子类和原始 CountryPicker 共享数据。您可以从 _countryNames_countryCodes_countryNamesByCode_countryCodesByName 中删除 static 关键字,它应该可以工作。