【问题标题】:change button image from UIPickerView从 UIPickerView 更改按钮图像
【发布时间】:2013-02-20 16:54:24
【问题描述】:

我有一个关于我一段时间以来一直在做的事情的问题。我有一个列出不同类型食物的 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 中实现这一点?

【问题讨论】:

  • 你试过什么?您知道如何从选择器视图中获取选择吗?你知道如何设置按钮的图像吗?

标签: ios objective-c uipickerview


【解决方案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,那么你真的需要两个对象吗?


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

-(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];
}

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

-(void)dismissPicker {
    [button1 setImage:[UIImage imageNamed: [[food objectAtIndex:row] objectAtIndex:1]] forState:UIControlStateNormal];
    [pickerActionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 工作完美,谢谢。我最终使用 [food1 setImage:[UIImage imageNamed:[foodImages objectAtIndex:[pickerView selectedRowInComponent:0]]] forState:UIControlStateNormal];因为它们在数组中的顺序相同
    【解决方案3】:

    实现UIPickerViewDelegate方法(docs):

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
        NSString *imageName = foodImages[row];
    
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    }
    

    设置你的选择器的委托:

    pickerView.delegate = self;
    

    然后将您的图像添加到您的 Xcode 项目中(如果您还没有这样做的话)。

    您可能还想为不同的状态设置图像,或者如果您还想在按钮上显示文字,请使用setBackgroundImage:forState(有关详细信息,请参阅UIButton docs)。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多