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

时间:2017-07-28 15:25:32

标签: ios objective-c uipickerview

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

我尝试过故事板,然后以编程方式启动2个选择器而没有成功。你可以看到我试图在显示第二个选择器之前删除第一个选择器,反之亦然,但它不会修复它。

即使我将 * countrySelectionPickerView 设置为 CountryPicker ,它仍然使用 CustomCountryPicker 中的overriden方法,这可能是什么原因?因此,对象的类型为 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];
}

1 个答案:

答案 0 :(得分:1)

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