gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:43:06 28 4
gpt4 key购买 nike

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

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

即使我将 *countrySelectionPickerView 设置为 CountryPicker,它仍然使用 CustomCountryPicker 的覆盖方法的原因可能是什么?所以对象的类型是CountryPicker,但它仍然显示简短的国家/地区列表。

自定义国家选择器.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 关键字,并且它应该工作。

关于ios - CountryPicker UIPickerView 调用子类方法而不是它自己的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45377227/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com