Using UIPickerView to display different images upon selection in UIImageView
所以我正在看这篇文章,以帮助我执行与此用户相同的功能。我正在使用PickerView让用户在三个不同的地图之间进行选择,并根据用户选择更改ImageView。我在代码的以下部分遇到了麻烦
chosenFloor.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[pickerImage objectAtIndex:row]]];
它告诉我'从UIImageView *分配给UIImage *的指针类型不兼容
有人可以解释如何转换UIImage以适应UIImageView吗?我将在下面包含我的其余代码。我相信我已经在我的.h文件中声明了所有内容,这是用户在上述链接中遇到的问题。
Screenshot of my iOS Simulator
#import <UIKit/UIKit.h>
@interface MapViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *chooseFloor;
IBOutlet UIImageView *chosenFloor;
NSArray *pickerData;
}
@property (nonatomic, retain) UIPickerView *chooseFloor;
@property (nonatomic, retain) UIImageView *chosenFloor;
@property (nonatomic, retain) NSArray *pickerFloor;
@property (nonatomic, retain) NSArray *pickerImage;
//-(IBAction)performSomeAction:(id)sender;
@end
#import "MapViewController.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize chooseFloor;
@synthesize pickerFloor;
@synthesize chosenFloor;
@synthesize pickerImage;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@"Basement",
@"Main Floor",
@"Penthouse", nil];
self.pickerFloor = array;
self.chooseFloor.dataSource = self;
self.chooseFloor.delegate = self;
self.pickerImage = [[NSArray alloc] initWithObjects:@"Basement.png", @"Main.png", @"Penthouse.png", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
//Determine how many columns in my pickerView
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
//Determine the number of rows in my pickerView
return [pickerFloor count];
}
//Delegate Method
//This delegate method gives us s way to retrieve the selected item from the picker view
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerFloor objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
chosenFloor.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[pickerImage objectAtIndex:row]]];
}
@end
答案 0 :(得分:3)
你错了:
chosenFloor.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[pickerImage objectAtIndex:row]]];
chosenFloor.image
是UIImage
。它应该是
chosenFloor.image = [UIImage imageNamed:[pickerImage objectAtIndex:row]];