0

单击文本字段时,我正在使用 inputView 显示 pickerView 。然后我想用数字 1-100 填充选择器视图。我有一些我认为可以工作的代码,但是当我运行程序并单击 textField 时,它只会弹出一个空的选取器视图。还需要知道如何在工具栏上放置另一个按钮。我想在右侧有一个提交按钮来确认用户的选择(将完成更改为取消)我必须在界面构建器中使用选择器和按钮做些什么吗?因为我实际上在界面构建器中没有这些,因为它们是用代码制作的......

下面是我的 .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];

}
}
4

1 回答 1

1

您没有将选取器的委托设置为当前对象类。

利用

`myPickerView.delegate = self;`

 `myPickerView.dataSource = self;`

分配选择器视图对象后。

继续..

于 2012-07-12T05:23:20.050 回答