gpt4 book ai didi

IOS & Objective C - 如何用国家列表填充 UIPickerView?

转载 作者:行者123 更新时间:2023-12-01 18:52:23 24 4
gpt4 key购买 nike

我是 iOS 开发新手(现在已经 2 周了),我正在开发一个 iOS 应用程序,我想填写 UIPickerView带有国家列表。

这是我到目前为止所做的:

1-创建选择器 View

2-将我的 View Controller 定义为选择器 View 的委托(delegate)

3-将这些代码行添加到我的 .m View Controller 文件中:(方法:viewdidload、numberOfComponentsInPickerView、titleForRow)

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]];

for (NSString *countryCode in [NSLocale ISOCountryCodes])
{
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
[countries addObject: country];
}
NSArray *sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;// or the number of vertical "columns" the picker will show...
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (sortedCountries!=nil) {
return [sortedCountries count];//this will tell the picker how many rows it has - in this case, the size of your loaded array...
}
return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//you can also write code here to descide what data to return depending on the component ("column")
if (sortedCountries!=nil) {
return [sortedCountries objectAtIndex:row];//assuming the array contains strings..
}
else
{
NSLog(@"Register tab empty?");
}
return @"empty";//or nil, depending how protective you are
}

这是我的 .h 文件:
#import <UIKit/UIKit.h>

@interface RegisterViewController_iPhone : UIViewController <UIPickerViewDelegate> {
NSArray * sortedCountries;
}

@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;

@end

当我运行时,它给了我一个空的 UIpickerView ,感谢您的帮助,谢谢。

最佳答案

此行不正确:

NSArray *sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

您正在重新定义 sortedCountries 而不是使用头文件中定义的那个。删除 NSArray * .

您的 viewDidLoad方法应如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]];

for (NSString *countryCode in [NSLocale ISOCountryCodes])
{
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
[countries addObject: country];
}
sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

}

关于IOS & Objective C - 如何用国家列表填充 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163951/

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