如何将 Web 服务中的数据填充到 UIPickerView 中? [iOS应用程序开发]

发布于 2024-11-29 03:35:32 字数 818 浏览 1 评论 0原文

任何人都知道如何将 Web 服务中的类别列表(例如类别列表)填充到 UIPickerView 中? 另外,是否可以输入隐藏值?

例子,
“公共”,隐藏 ID 为“1”
“Member”的隐藏 ID 为“2”等等。

这 2 个值来自网络服务。顺便说一句,我正在使用 ASIHTTPRequest 框架。

我是 iOS/XCode/Objective C 的新手。希望有人能够提供帮助):谢谢

=============== 回复 =========== =====

@MSgambel
好吧,尽力解释一下。就目前而言,我们如何将项目放入 UIPickerView 中是:

[NSArray arrayWithObjects:@"John Appleseed", @"Chris Armstrong", @"Serena Auroux", @"Susan Bean", @"Luis Becerra", @"Kate Bell ”,@“阿兰·布里埃”,nil];

除了上面的那些项目之外,我如何将 Web 服务中的项目填充到选择器中?

@阿克谢
我所说的隐藏的意思是选择器将有 2 个值,但选择器上只有标签“Public”可见,而不是 id“1”。我想实现它有点类似于网站上的下拉菜单。

在名为“Domain”的数据库中,我们有以下格式的值:
域名 ID : 域名
1:公共
2:A公司
3:B公司
将有另一个数据库“Users”,它与 DomainID 绑定。 (仅举一个例子)


选择器将在界面上显示“公共”,当用户选择它时,它的值为“1”。那么我可以做这样的事情吗?

Anyone knows how to populate, example a list of categories, from web service into the UIPickerView?
Also, is it possible to put in a hidden value?

Example,

"Public" with hidden id of "1"

"Member" with hidden id of "2" and so on..

Those 2 values are to be from the webservice. I'm using the ASIHTTPRequest framework by the way.

I'm a newbie in iOS/XCode/Objective C. Hope someone is able to help ): Thank you

=============== Response ================

@MSgambel
Alright, try my best to explain. As for now, how we put items into UIPickerView is:

[NSArray arrayWithObjects:@"John Appleseed", @"Chris Armstrong", @"Serena Auroux", @"Susan Bean", @"Luis Becerra", @"Kate Bell", @"Alain Briere",nil];

instead of those items above, how do I populate items from the webservice into the picker?

@Akshay
What I meant by hidden is that the picker will have 2 values, but only the label "Public" is visible on the picker and not the id "1". I would like to implement it somewhat similar to a dropdown menu on websites.

In a database called "Domain" we have values in the following format:

DomainID : DomainName

1 : Public

2 : CompanyA

3 : CompanyB

There will be another database "Users" where it is tied to the DomainID. (Just an example)

The picker will show "Public" on the interface, when user selects it, it will have a value of "1". So is there anyway I could do such a thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2024-12-06 03:35:32

首先,您没有提供有关您的网络服务的详细信息。假设它是基于 SOAP 的,那么我强烈推荐 sudzcwsd2lobjc。这些将为您生成代码,以便将 WSDL def 很好地转换为 Objective C 类。

至于 UIPickerView“隐藏”值的问题,这个问题非常简单。创建一个 NSDictionariesNSArray。使用您的示例(假设都是字符串):

NSDictionary *dictOne = [NSDictionary DictionaryWithObjectsAndKeys:@"1",@"realValue",@"Public",@"looksGoodValue",nil];
NSDictionary *dictTwo = [NSDictionary DictionaryWithObjectsAndKeys:@"2",@"realValue",@"CompanyA",@"looksGoodValue",nil];
NSDictionary *dictTre = [NSDictionary DictionaryWithObjectsAndKeys:@"3",@"realValue",@"CompanyB",@"looksGoodValue",nil];

array = [NSArray arrayWithObjects:dict1,dict2,dict3, nil];

并在 UIPickerViewDelegate 方法中

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

return [[self.array objectAtIndex:row] objectForKey:@"looksGoodValue"];

}

最后在您使用 pickerview 的任何内容(例如您的网络服务)

request.realValue = [[self.array objectAtIndex:[pickerView selectedRowInComponent:0]] valueForKey:@"realValue" ];

希望这有帮助!

First of all, you haven't provided a lot of detail on the details of your web service. Assuming it is SOAP based then I highly recommend sudzc or wsd2lobjc. These will generate code for you to convert your WSDL def into Objective C classes quite nicely.

As for the question of UIPickerView "hidden" values this one is pretty simple. Create an an NSArray of NSDictionaries. Using your example (assuming all are strings):

NSDictionary *dictOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"realValue",@"Public",@"looksGoodValue",nil];
NSDictionary *dictTwo = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"realValue",@"CompanyA",@"looksGoodValue",nil];
NSDictionary *dictTre = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"realValue",@"CompanyB",@"looksGoodValue",nil];

array = [NSArray arrayWithObjects:dict1,dict2,dict3,nil];

and in your UIPickerViewDelegate method

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

return [[self.array objectAtIndex:row] objectForKey:@"looksGoodValue"];

}

and finally in whatever you use the pickerview for (say your web service)

request.realValue = [[self.array objectAtIndex:[pickerView selectedRowInComponent:0]] valueForKey:@"realValue"];

Hope this helps!

飘落散花 2024-12-06 03:35:32

在使用 ASIHTTPRequest 加载这些值后,我会将这些值保存在 propertyList 中。要保存文件,请使用

    `NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

      NSString *documentsDirectory = [paths objectAtIndex:0];

      NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

         NSMutableArray *array = [[NSMutableArray alloc] init];
       [array addObject:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",stringOne],//像这样添加所有字符串 nil]];
       [array writeToFile:pathatomically:YES];`

然后使用 [NSMutableArray alloc] initWithContentsOfFile:path] 将您的值加载到 pickerView 中。

I would save those values in a propertyList, after you have loaded them using ASIHTTPRequest. To save the files use

    `NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

     NSString *documentsDirectory = [paths objectAtIndex:0];

     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

         NSMutableArray *array = [[NSMutableArray alloc] init];
        [array addObject:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",stringOne],//add all of your strings like that nil]];
        [array writeToFile:path atomically:YES];`

Then use [NSMutableArray alloc] initWithContentsOfFile:path] to load your values into the pickerView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文