gpt4 book ai didi

ios - 如何从 iOS 中的操作表中获取 pickervalue?

转载 作者:行者123 更新时间:2023-11-28 22:52:01 25 4
gpt4 key购买 nike

在我的应用程序中,当按下按钮时,我将启动带有 UIPickerViewUIToolBarUIActionSheet。它通过使用以下代码完美地工作。 我正在尝试获取选择器值并将该值设置为被调用的按钮,但我找不到确切的解决方案。

- (IBAction)showPicker:(id)sender {

actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];//as we want to display a subview we won't be using the default buttons but rather we're need to create a toolbar to display the buttons on

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
//[pickerView release];

pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];


[pickerToolbar setItems:barItems animated:YES];

[actionSheet addSubview:pickerToolbar];

[actionSheet addSubview:pickerView];

[actionSheet showInView:self.view];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

当按下工具栏中的完成按钮时,它将调用此方法

-(void)doneButtonPressed:(id)sender{  
//Do something here here with the value selected using [pickerView date] to get that value
[actionSheet dismissWithClickedButtonIndex:1 animated:YES];
[pickerView release];
[pickerToolbar release];
[actionSheet release];
}

这是我的问题

如何从 action sheet 中获取 picker 的值?

如何给按钮设置那个值?

谢谢大家的帮助

最佳答案

假设 pickerView 数据源只有一个组件,您可以使用以下方法获取所选行:

NSInteger row = [pickerView selectedRowInComponent:0];

关于ios - 如何从 iOS 中的操作表中获取 pickervalue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11754985/

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