gpt4 book ai didi

ios - 从 UIPickerView 更改按钮图像

转载 作者:行者123 更新时间:2023-11-28 20:23:33 25 4
gpt4 key购买 nike

我对我已经研究了一段时间的事情有疑问。我有一个列出不同类型食物的 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"];

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

最佳答案

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

你可以这样做:

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 并调用 [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];
}

上面的代码制作了一个漂亮的选择器 View ,带有一个取消和确定按钮作为工具栏。您需要的最终功能是(假设您有一个名为“button1”的 UIButton):

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

关于ios - 从 UIPickerView 更改按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com