Reputation: 109
I am using the inputView to display a pickerView when a a textfield is clicked. I then want to populate the pickerview with the numbers 1-100. I have some code that I thought would work, but when i run the program and click on the textField, it just pops up an empty pickerview. Also need to know how to put another button on the toolbar. I would like to have a submit button on the right side to confirm the users selection (going to change the done to cancel) Is there something I have to do in the interface builder with the picker and buttons? because i don't actually have those in the interface builder because they are made with code...
Below are my .h and .m files.
.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];
}
}
Upvotes: 0
Views: 619
Reputation: 3506
You are not setting delegate of picker to the current object class.
use
`myPickerView.delegate = self;`
`myPickerView.dataSource = self;`
after allocating the picker view object.
go on..
Upvotes: 1