ios - 尝试使用 UIPickerView 选择要在 UIImageView 中显示的图像

标签 ios objective-c iphone uiimageview uiimage

Using UIPickerView to display different images upon selection in UIImageView

因此,我正在查看这篇文章以帮助我执行与该用户相同的功能。我正在使用 PickerView 让用户在三个不同的 map 之间进行选择,并根据用户选择更改 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

最佳答案

你在这一行是错的:

 chosenFloor.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[pickerImage objectAtIndex:row]]];
chosenFloor.imageUIImage .它应该是
chosenFloor.image = [UIImage imageNamed:[pickerImage objectAtIndex:row]];

关于ios - 尝试使用 UIPickerView 选择要在 UIImageView 中显示的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072679/

相关文章:

ios - 根据内容动态调整 UITextView 的高度?

ios - 使用 Storyboard,如何判断用户何时离开场景?

objective-c - 如何根据另一个实体的一对多关系访问 CoreData 实体?

iphone - iPad 和 iPhone 上 HTML 电子邮件中的表格边框

iphone - 如何测试股票市场 (NYSE) 当前是否开盘/收盘?

ios - VerificationController 突然停止为所有应用程序工作

ios - bundle 无效——Info.plist 中的 CFBundleVersion 和 CFBundleShortVersionString 必须包含更高版本——但它们确实如此

ios - 在重用的原型(prototype)单元格中多次应用渐变蒙版

iphone - 未调用委托(delegate)方法 "clickedButtonAtIndex:"

iphone - iPhone 核心数据新功能