合适的人,我是xcode和iPhone SDK的新手,但是在过去的几周里一直保持着它,慢慢地到达那里,但是我刚刚开始构建一个使用UIPickerView的应用程序,到目前为止我有一个具有7个值的选择器。当选择一个值时,它会显示在一个标签内......非常简单的东西。但到目前为止,我找不到任何教程,其中包括如何加载标准UIImageView然后我可以根据选择器中选择的内容交换图像。(每个人的马克杯镜头)你们现在在哪里,我可以找到一些帮助这或指向我正确的方向.....
到目前为止,我的代码非常标准:
我在.h文件中的: -
#import <UIKit/UIKit.h>
@interface Team : UIViewController
{
IBOutlet UIPickerView *pickerView;
NSMutableArray *list;
IBOutlet UILabel *pickerLabel;
IBOutlet UIImage *pickerImage;
}
@end
在.m文件中我有: -
#import "Team.h"
@implementation Team
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{
return [list count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *string = [NSString stringWithFormat:@"You Selected %@", [list objectAtIndex:row]];
pickerLabel.text = string;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
list = [[NSMutableArray alloc]init];
[list addObject:@"Doug Filder"];
[list addObject:@"Chris Savage"];
[list addObject:@"Nick Bennett"];
[list addObject:@"Aimee Vacher"];
[list addObject:@"Brandy Cardwell"];
[list addObject:@"Jon West"];
[list addObject:@"Dan Parsons"];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
答案 0 :(得分:1)
您可以使用NSDictionary来存储人员的姓名并关联文件名, 例如:
首先,将UIImage更改为UIImageView:
@interface Team : UIViewController
{
IBOutlet UIPickerView *pickerView;
NSMutableArray *list;
NSMutableDictionary *filenames;
IBOutlet UILabel *pickerLabel;
IBOutlet UIImageView *pickerImage;
}
@end
然后,这是你如何将图像加载到其中:
- (void)viewDidLoad {
list = [[NSMutableArray alloc]init];
[list addObject:@"Doug Filder"];
[list addObject:@"Chris Savage"];
[list addObject:@"Nick Bennett"];
[list addObject:@"Aimee Vacher"];
[list addObject:@"Brandy Cardwell"];
[list addObject:@"Jon West"];
[list addObject:@"Dan Parsons"];
filenames = [[NSMutableDictionary alloc] init];
[filenames setObject:@"DougFilder.jpg" forKey:@"Doug Filder"];
[filenames setObject:@"ChrisSavage.jpg" forKey:@"Chris Savage"];
[filenames setObject:@"NickBennett.jpg" forKey:@"Nick Bennett"];
[filenames setObject:@"AimeeVacher.jpg" forKey:@"Aimee Vacher"];
[filenames setObject:@"BrandyCardwell.jpg" forKey:@"Brandy Cardwell"];
[filenames setObject:@"JonWest.jpg" forKey:@"Jon West"];
[filenames setObject:@"DanParsons.jpg" forKey:@"Dan Parsons"];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *string = [NSString stringWithFormat:@"You Selected %@", [list objectAtIndex:row]];
pickerLabel.text = string;
NSString *filename = [filenames objectForKey:[list objectAtIndex:row]];
[pickerImage setImage:[UIImage imageNamed:filename]];
}
希望这有帮助! :)
答案 1 :(得分:1)
只需替换此
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *string = [NSString stringWithFormat:@"You Selected %@", [list objectAtIndex:row]];
pickerLabel.text = string;
pickerImage = [UIImage imageNamed:string];
}