我使用 inputView 在单击文本字段时显示 pickerView。然后我想用数字 1-100 填充 pickerview。我有一些我认为可以工作的代码,但是当我运行程序并单击文本字段时,它只会弹出一个空的选择器 View 。还需要知道如何在工具栏上放置另一个按钮。我想在右侧有一个提交按钮来确认用户的选择(将更改完成以取消)我必须在界面生成器中使用选择器和按钮执行某些操作吗?因为我实际上在界面生成器中没有这些,因为它们是用代码制作的......
下面是我的 .h 和 .m 文件。
.h----
@interface TestinputviewViewController : UIViewController <UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate> {
IBOutlet UITextField *textField;
NSMutableArray *pickerViewArray;
}
-(IBAction) textFieldDidBeginEditing;
@end
.m---
#import "TestinputviewViewController.h"
@implementation TestinputviewViewController
-(IBAction)textFieldDidBeginEditing{
UIPickerView *myPickerView = [[UIPickerView alloc] init];
textField.inputView = myPickerView;
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
[myPickerView release];
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(inputAccessoryViewDidFinish)];
[myToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
textField.inputAccessoryView = myToolbar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myPickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *) myPickerView numberOfRowsInComponent: (NSInteger)component {
return [pickerViewArray count];
}
-(NSString *)pikerView:(UIPickerView *) myPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerViewArray objectAtIndex:row];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
pickerViewArray = [[NSMutableArray alloc] init];
for (int i = 1; i<=20; i++) {
NSString *myString = [NSString stringWithFormat:@"%d%",i];
[pickerViewArray addObject:myString];
}
}
最佳答案
您没有将选择器的委托(delegate)设置为当前对象类。
使用
`myPickerView.delegate = self;`
`myPickerView.dataSource = self;`
分配选取器 View 对象后。
继续..
关于ios - 无法使用我的字符串填充 PickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11445331/