0

我有一个关于我一段时间以来一直在做的事情的问题。我有一个列出不同类型食物的 UIPickerView。我的数组看起来像这样:

food = [[NSMutableArray alloc] init];
    [activities addObject:@"Pasta"];
    [activities addObject:@"Pizza"];
    [activities addObject:@"Spaghetti"];
    [activities addObject:@"Salad"];

我的图像数组是:

foodImages = [[NSMutableArray alloc] init];
        [activities addObject:@"Pasta.png"];
        [activities addObject:@"Pizza.png"];
        [activities addObject:@"Spaghetti.png"];
        [activities addObject:@"Salad.png"];

我想知道的是,当用户从选择器视图中选择一个项目时,我是否可以更改按钮的图像。因此,如果从 UIPickerView 中选择了意大利面,它会将按钮的图像更改为“Pasta.png”。我怎样才能在 Xcode 中实现这一点?

4

4 回答 4

1

Implement the UIPickerViewDelegate method (docs):

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSString *imageName = foodImages[row];

    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

}

Set your picker's delegate:

pickerView.delegate = self;

Then add your images to your Xcode project (if you haven't done so already).

You may also want to set the image for different states, or use setBackgroundImage:forState if you also want text on the button (see UIButton docs for more info).

于 2013-02-20T17:02:19.410 回答
1

你当然可以这样做。首先,将图像链接和食物名称存储在一个数组中会更好。这将使您的应用程序更具可扩展性,并且对代码的更改不太可能导致错误。

你可以这样做:

food = [[NSMutableArray alloc] init];
[food insertObject:[NSMutableArray arrayWithObjects:@"Pasta",@"Pasta.png",nil] atIndex:[food count]];
[food insertObject:[NSMutableArray arrayWithObjects:@"Pizza",@"Pizza.png",nil] atIndex:[food count]];

虽然如果你的图片和你的名字一样但是扩展名为 .png 那么你真的需要两个对象吗?


至于为 a 设置图像,UIPicker你会做这样的事情(你需要添加UIPickerViewDelegate到 .h 并调用[self setPickerValue];你想显示的地方UIPicker- 也许是一个按钮或文本字段?):

-(void)setPickerValue {
    pickerActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    [pickerActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
    UIPickerView *valuesPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];

    [valuesPicker setDataSource: self];
    [valuesPicker setDelegate: self];
    valuesPicker.showsSelectionIndicator = YES;
    [pickerActionSheet addSubview:valuesPicker];

    UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [controlToolBar setBarStyle:UIBarStyleBlack];
    [controlToolBar sizeToFit];

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Set" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPicker)];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelPicker)];
    [controlToolBar setItems:[NSArray arrayWithObjects:cancelButton, spacer, setButton, nil] animated:NO];

    [pickerActionSheet addSubview:controlToolBar];
    [pickerActionSheet showInView:self.view];
    [pickerActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)cancelPicker {
    [pickerActionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
// Number of components.
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
// Total rows in our component.
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [food count];
}
// Display each row's data.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [[food objectAtIndex: row] objectAtIndex:0];
}

上面的代码创建了一个不错的选择器视图,其中带有一个取消和确定按钮作为工具栏。您需要的最后一个功能是(假设您有一个UIButton名为“button1”):

-(void)dismissPicker {
    [button1 setImage:[UIImage imageNamed: [[food objectAtIndex:row] objectAtIndex:1]] forState:UIControlStateNormal];
    [pickerActionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
于 2013-02-20T17:00:10.780 回答
1

[yourButton setImage:[UIImage imageNamed:[foodImages objectAtIndex:0]] forState:UIControlStateNormal];// objectAtIndex 0 将返回“Pasta.PNG”,1 将返回 Pizza 等。

于 2013-02-20T17:00:20.043 回答
0

我会使用一个 NSDictionary,其中食物的名称是键,食物的图片文件是值。然后当它被选中时,只需加载Key的值作为图片。

如果你想坚持使用两个数组,那么获取所选食物的索引,只要你的 foodImages 数组镜像食物数组,你就可以使用 objectAtIndex 的值来加载图像。

于 2013-02-20T17:05:29.133 回答