gpt4 book ai didi

ios - UIPickerview 完成按钮不起作用,即使它没有调用选择器方法

转载 作者:行者123 更新时间:2023-12-01 16:03:18 25 4
gpt4 key购买 nike

我正在创建 UIPickerView programatically并添加了自定义 button , 当用户按下 button , picker不会显示。但是当我按下 button ,它甚至没有调用它的selector method .我知道这个问题在这个网站上有很多答案,但它并没有解决我的问题。

-(void) funtion
{
self.myPickerView = [[UIPickerView alloc]init];
self.myPickerView.dataSource = self;
self.myPickerView.delegate = self;
self.myPickerView.showsSelectionIndicator = YES;
self.myPickerView.frame = CGRectMake(0, 417, 320, 151);
[self.myPickerView setBackgroundColor:[UIColor whiteColor]];

UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(donePressed:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];


[self.myPickerView addSubview:toolBar];

[self.view addSubview:self.myPickerView];

}

-(void)donePressed:(id)sender
{
[self.myPickerView endEditing:YES];
}

最佳答案

例如我添加这个

第一步

创造一个 UIPicker , UItextField和 NSArray 用于加载详细信息

@interface ViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{

UITextField *myTextField;
UIPickerView *myPickerView;
NSArray *pickerArray;
}

@end

第二步

在您的 ViewDidLoad 上像这样调用选择器方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addPickerView];
}

第三步
create the picker, textfield and done button

-(void)addPickerView{
pickerArray = [[NSArray alloc]initWithObjects:@"Chess",
@"Cricket",@"Football",@"Tennis",@"Volleyball", nil];
myTextField = [[UITextField alloc]initWithFrame:
CGRectMake(10, 100, 300, 30)];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.delegate = self;
[self.view addSubview:myTextField];
[myTextField setPlaceholder:@"Pick a Sport"];
myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:@"Done" style:UIBarButtonItemStyleDone
target:self action:@selector(done:)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
myPickerView.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
myTextField.inputView = myPickerView;
myTextField.inputAccessoryView = toolBar;

}

第四步

如果用户按下完成按钮退出选择器
-(void)done:(id)sender
{

[myTextField resignFirstResponder];
}

第五步

PickerView 委托(delegate)方法
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count];
}

#pragma mark- Picker View Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
[myTextField setText:[pickerArray objectAtIndex:row]];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)row forComponent:(NSInteger)component{
return [pickerArray objectAtIndex:row];
}

在这里我附上了 sample project

示例输出如

enter image description here

更新

如果你使用按钮隐藏pickerview使用这个
-(void)donePressed:(id)sender
{
[self.myPickerView removeFromSuperview];

}

当您按下按钮时调用此方法
[self funtion];

关于ios - UIPickerview 完成按钮不起作用,即使它没有调用选择器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35081621/

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