我已经在按钮点击操作上编写了以下代码,以便创建ui选择器视图
- (IBAction)selectbtn:(id)sender {
pickerview = [[UIPickerView alloc] initWithFrame:CGRectMake(5, 40, 300, 300)];
pickerview.showsSelectionIndicator = YES;
pickerview.hidden = NO;
pickerview.delegate = self;
[self.view addSubview:pickerview];
}
以下是我的选择代表
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.jsonresultarr.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// return [[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Id"];
// NSObject *companyId = [[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Id"];
NSObject *companyName = [[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Name"];
return [NSString stringWithFormat:@"%@", companyName, nil];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.Textbox.text=[[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Name"];
[self.pickerview removeFromSuperview];
}
我的问题是我想在uipicker视图上显示完成按钮,其功能是在我的文本字段中设置选定的值。我怎么能做到。 Thanx提前
答案 0 :(得分:9)
你可以用它。首先在你的self.view中添加UIView。添加UIPickerView
和UIToolBar
init。并将IBAction方法与UIBarButtonItem绑定。请检查附加屏幕截图。
也许这会对你有帮助。
答案 1 :(得分:2)
你应该使用UIBarButtonItem
&& UIToolbar
// add a toolbar with Done button
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
self.pickerViewTextField.inputAccessoryView = toolBar;
点击完成按钮时触发了选择器视图
- (void)doneTouched:(UIBarButtonItem *)sender
{
// hide the picker view
[self.pickerViewTextField resignFirstResponder];
// perform some action
}
您可以在didSelectRow
委托方法中获取所选值。
答案 2 :(得分:1)
试试这个
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(done)];
cancelButton=[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
[cancelButton setTintColor:[UIColor whiteColor]];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-PickerView.frame.size.height-50, 320, 50)];
NSArray *toolbarItems = [NSArray arrayWithObjects:cancelButton,doneButton,nil];
[toolbar setItems:toolbarItems];
self.textfield1.inputAccessoryView = toolbar;
其中,完成方法如下
-(void)done{
self.textfield1.text =[Array objectAtIndex:];
// for you it
//self.Textbox.text=[[self.jsonresultarr objectAtIndex:[PickerView selectedRowInComponent:0]] objectForKey:@"Company_Name"];
}
和取消方法是
-(void)cancel{
[self.view endEditing:YES];
}
答案 3 :(得分:-1)
您可以简单地使用此方法(对于ios8)
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 0, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
txtPriorityTemp=myTextField;
[actionSheet addSubview:pickerView];
alertController = [UIAlertController alertControllerWithTitle:nil
message:@"\n\n\n\n\n\n\n\n\n\n"
preferredStyle:UIAlertControllerStyleActionSheet];
//pickerView.frame=CGRectMake(0, 0, 120, 30);
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305.0f, 44.f)];
toolView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blank-headerNew.png"]];
CGRect buttonFrame = CGRectMake(220, 5, 70, 30);
UIButton *selButton = [[UIButton alloc] initWithFrame:buttonFrame];
//[selButton setBackgroundImage:[UIImage imageNamed:@"red_bgnew.png"] forState:UIControlStateNormal];
[selButton setTitle:@"Done" forState:UIControlStateNormal];
[selButton addTarget: self
action: @selector(dismissActionSheet:)
forControlEvents: UIControlEventTouchDown];
selButton.tag= myTextField.tag+10;
[toolView addSubview:selButton];
[alertController.view addSubview:pickerView];
[alertController.view addSubview:toolView];
[self presentViewController:alertController animated:YES completion:nil];
}