Im trying to learn how to select an object from a picker view to display in a TextBox, six hours in I thought id finally cracked but no. It works, but I can only get the first item in my NSArray to display in my text box.
Select
being my picker, arrstatus
is my NSArray, text1
is my text box.
Using a button to get my value: under IBAction:
NSInteger selectedRow = [select selectedRowInComponent:0];
text1.text = [arrStatus objectAtIndex:selectedRow];
[text1 resignFirstResponder];
rest of code:
.h
@interface pick5 : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate,UITextFieldDelegate>
{
UIPickerView *select;
NSArray *arrStatus;
}
.m
UIPickerView *select = [[UIPickerView alloc] initWithFrame:CGRectZero];
select.delegate = self;
select.dataSource = self;
[select setShowsSelectionIndicator:YES];
text1.inputView = select;
arrStatus = [[NSArray alloc] initWithObjects:@"bs88", @"bs60898", @"Memphis", @"California", @"Seattle",@"London",@"Paris", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return arrStatus.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//set item per row
return [arrStatus objectAtIndex:row];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setText1:nil];
[self setResign:nil];
[self setText2:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)resign:(id)sender {
NSInteger selectedRow = [select selectedRowInComponent:0];
text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];
}
@end
im also getting yellow warning triangles local decalration of "select" hides instance variable
For your warning in yellow color change the following line of code in your .m file.
UIPickerView *select = [[UIPickerView alloc] initWithFrame:CGRectZero];
replace above with below:
select = [[UIPickerView alloc] initWithFrame:CGRectZero];
because you have already declared uipicker* select in .h file.