如何在 PickerView 顶部添加按钮以及如何在单击该按钮时关闭 PickerView?

发布于 2024-12-20 20:48:58 字数 76 浏览 3 评论 0原文

我有一个 UIPickerView。我想要在选择器视图顶部有一个完成按钮,并且我想在单击完成按钮时关闭选择器视图。你能帮我解决这个问题吗?

I have a UIPickerView. I want a done button on top of the pickerview and i want to dismiss the picker view on click of the done button. Can u please help me on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没︽人懂的悲伤 2024-12-27 20:48:58

将操作表添加到您的视图中,然后添加带有完成按钮的工具栏
在操作表的顶部和下面将您的选择器添加到操作表
在“完成”按钮中,单击下面的方法来关闭操作表

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];

actionSheet=[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
[pickerToolbar sizeToFit];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
    [barItems addObject:cancelBtn];
    [cancelBtn release];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
    [barItems addObject:doneBtn];
    [doneBtn release];
    [pickerToolbar setItems:barItems animated:YES];
    [actionSheet addSubview:pickerToolbar];
    [barItems release];
    [pickerToolbar release];

UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 216);
picker.delegate  = self;
        picker.dataSource = self;
        picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
[picker release];

-(void)done_clicked:(id)sender
{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel_clicked:(id)sender
{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

add action sheet to your view and then add tool bar with done button
at top of action sheet and below add your your picker to action sheet
in done button click write below method to dismiss action sheet

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];

actionSheet=[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
[pickerToolbar sizeToFit];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
    [barItems addObject:cancelBtn];
    [cancelBtn release];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
    [barItems addObject:doneBtn];
    [doneBtn release];
    [pickerToolbar setItems:barItems animated:YES];
    [actionSheet addSubview:pickerToolbar];
    [barItems release];
    [pickerToolbar release];

UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 216);
picker.delegate  = self;
        picker.dataSource = self;
        picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
[picker release];

-(void)done_clicked:(id)sender
{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel_clicked:(id)sender
{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
此刻的回忆 2024-12-27 20:48:58

您可以使用工具栏来达到相同的目的。查看此链接 。这里,考虑相同的解决方案。希望这可以帮助你。谢谢。

You can use the toolbar for the same purpose. Take a look at this link. Here, the same solution is taken into consideration. Hope that may help you. Thanks.

丿*梦醉红颜 2024-12-27 20:48:58

每个主体都采用操作表,然后添加栏按钮,但只是从视图中添加 uibutton 并采取方法从超级视图中删除。显示以下示例,我尝试此代码成功。我认为这是非常有用的代码,无需使用操作表

 -(void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(@"the marray data is %@",marray); // [self showpicker1]; if (marray.count == 0) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"NO DATA FOUND" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; }

[self showpicker1];
}

-(void)showpicker1 { //view1=[[UIView alloc] initWithFrame:CGRectMake(55, 59, 171, 80)]; //[self.view addSubview:view1]; pv = [[UIPickerView alloc] initWithFrame:CGRectMake(55,79,171,0)]; pv.delegate = self; pv.dataSource = self; pv.showsSelectionIndicator = YES; [self.view addSubview:pv]; done=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [done setFrame:CGRectMake(55, 59, 171, 20)]; [done setTitle:@"DONE" forState:UIControlStateNormal]; [done addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:done]; } -(void)dismiss { [pv removeFromSuperview]; [done removeFromSuperview]; }

every body took the action sheet and then added the bar button ,but simply added uibutton from the view and take method remove from the super view. show the bellow example , i am tried this code successful. i think this is very useful code with out using action sheet

 -(void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(@"the marray data is %@",marray); // [self showpicker1]; if (marray.count == 0) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"NO DATA FOUND" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; }

[self showpicker1];
}

-(void)showpicker1 { //view1=[[UIView alloc] initWithFrame:CGRectMake(55, 59, 171, 80)]; //[self.view addSubview:view1]; pv = [[UIPickerView alloc] initWithFrame:CGRectMake(55,79,171,0)]; pv.delegate = self; pv.dataSource = self; pv.showsSelectionIndicator = YES; [self.view addSubview:pv]; done=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [done setFrame:CGRectMake(55, 59, 171, 20)]; [done setTitle:@"DONE" forState:UIControlStateNormal]; [done addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:done]; } -(void)dismiss { [pv removeFromSuperview]; [done removeFromSuperview]; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文