从 Apple iPhone SDK 中的 UIPickerview 选择数据时如何显示图像
Posted
技术标签:
【中文标题】从 Apple iPhone SDK 中的 UIPickerview 选择数据时如何显示图像【英文标题】:How to display images when selecting data from a UIPickerview in Apple's iPhone SDK 【发布时间】:2011-12-03 16:43:36 【问题描述】:我有一个多组件选择器,带有两个单独的***,显示相关信息。我想根据选择的组件显示图像。例如:如果在两个***中都选择了“AA”,则会显示图像 AA。如果将右轮改为“B”,则会显示图像 AB。
提前感谢您的帮助。
问候,
【问题讨论】:
【参考方案1】:你可以这样做:
#import <UIKit/UIKit.h>
@interface PickerController: UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
NSArray *titleStrings;
UIPickerView *picker;
UIImage *imageView;
@end
@implementation PickerController
- (id) init
if (self = [super init])
titleStrings = [[NSArray alloc] initWithObjects:@"A", @"B", NULL];
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
picker.showsSelectionIndicator = YES;
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty.png"]];
imageView.frame = CGRectMake(0, 200, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
[imageView release];
[picker release];
return self;
- (void) dealloc
[titleStrings release];
[super dealloc];
/* UIPickerViewDataSource */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 2;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
return 2;
/* UIPickerViewDelegate */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
return [titleStrings objectAtIndex:row];
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
NSString *first = [titleStrings objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *second = [titleStrings objectAtIndex:[pickerView selectedRowInComponent:1]];
NSString *imageName = [[NSString alloc] initWithFormat:@"%@%@", first, second];
imageView.image = [UIImage imageNamed:imageName];
[imageName release];
[imageView sizeToFit];
@end
【讨论】:
对不起,我对编码很陌生,我很挣扎!你能看看我的代码并告诉我我需要做什么。另外,我如何像上面那样上传代码?谢谢 如果您无法将代码复制并粘贴到编辑器中,那么您还有很多东西要学。我敢打赌,如果你只是用默认的 main.m 文件复制到 Xcode 中,你应该会得到一个合理的结果。以上是关于从 Apple iPhone SDK 中的 UIPickerview 选择数据时如何显示图像的主要内容,如果未能解决你的问题,请参考以下文章
iPhone SDK 3.2 测试版和 iPhone SDK 3.1.2