0

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];

}
}
user1450221
  • 109
  • 2
  • 12
  • did u made the **pickerViewArray** as a subview of TextField?. – New Xcoder Jul 12 '12 at 05:20
  • No, I'm not sure what you mean. @New Xcoder – user1450221 Jul 13 '12 at 01:08
  • Just put the picker array in viewdidload method and call the array in function.. – New Xcoder Jul 16 '12 at 10:53
  • I put the picker array underneath viewdidLoad. I have posted an update to my original question with the new code. Please look at it. I am still just getting question marks in the picker. In my array, if i change the number in this part of code (i<=20) if i change 20 to another number it will change the number of question marks. @NewXcoder – user1450221 Jul 17 '12 at 17:33
  • I just figured it out!!! there was a typo in the titleForRow...I had pikerView instead of pickerView!! Thanks for the help! @NewXcoder – user1450221 Jul 17 '12 at 17:40

1 Answers1

1

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..

Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
  • He also needs to set his datasource. – tomidelucca Jul 12 '12 at 05:40
  • I used the code you gave me, now when the pickerview pop up it just shows question marks in it instead of numbers. Am I not coding the 'pickerviewarray right? – user1450221 Jul 12 '12 at 18:31
  • I know that if i had the pickerview actually displayed on the main view without a button, i would code the array under viewdidload. But i tried that and it did nothing. – user1450221 Jul 12 '12 at 20:00