gpt4 book ai didi

objective-c - UIPickerView 在 ActionSheet 上的奇怪行为

转载 作者:行者123 更新时间:2023-11-28 20:32:04 24 4
gpt4 key购买 nike

- (void) actionsheetstart
{
actionSheet=[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 480, 32)];
[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];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];

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

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

出于某种原因,操作表很好,但 UIPickerView 和 UIToolBar 变得疯狂了。我尝试将框架设置为 -px 但没有成功。似乎是什么问题?

非常感谢。

dismissing pickerview

最佳答案

不要在 UIActionSheet 中插入 UIPickerView,这样您将无法获得正确的行为。我的解决方案:

  1. 子类UIButton,实现方法canBecomeFirstResponder:,返回YES
  2. 在子类UIButton中实现inputView方法,返回UIPickerView
  3. 可选地,在您的子类按钮中实现 inputAccessoryView 方法以返回选择器上方的工具栏。
  4. 让您的按钮成为FirstResponder:被点击时。

瞧,当点击按钮时,你的选择器会像键盘一样模态显示。

编辑:

MyButton.h

#import <UIKit/UIKit.h>

@interface MyButton : UIButton

@end

我的按钮.m

#import "MyButton.h"

@implementation MyButton {
UIDatePicker *_pv;
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

#pragma mark - Picker

- (UIView *)inputView {
// set up your UIPickerView here
_pv = [[UIDatePicker alloc] init];
[_pv setDatePickerMode:UIDatePickerModeTime];
[_pv setMinuteInterval:30];
return _pv;
}

- (UIView *)inputAccessoryView {
// set up your toolbar here
UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *ok = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pick:)];
[tb setItems:[NSArray arrayWithObjects:flex, ok, nil]];
return tb;
}

- (void)pick:(id)sender {
// store picker value here before closing inputView
[self resignFirstResponder];
}
@end

关于objective-c - UIPickerView 在 ActionSheet 上的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815512/

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