I have an application in which I have a textfield where I am displaying values in it from database. There is a button besides the textfield. I have picker which contain 4 values in it viz Custom,Walk,Run,Jog.
If the textfield contains a value i.e if contains 'Walk' in it then on the click of button I am showing the picker with the 4 values populated in it. But my problem is when the button is clicked the value 'Walk' should be set on the picker.
This is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
currentarray = [[NSMutableArray alloc] initWithObjects:@"Custom",@"Walk",@"Run",@"Jog",nil];
// Do any additional setup after loading the view from its nib.
}
This is my button action which when clicked will open the picker:
-(IBAction)choose
{
actionsheet = [[UIActionSheet alloc]initWithTitle:[currentarray objectAtIndex:0] delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[actionsheet setTitle:@"Picker"];
pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 0, 0)];
pickerView.dataSource=self;
pickerView.delegate=self;
pickerView.showsSelectionIndicator=YES;
[actionsheet addSubview:pickerView];
[actionsheet showInView:self.view];
[pickerView selectRow:[txtTextfield.text intValue ]-1 inComponent:0 animated:YES];
[actionsheet showInView:[[UIApplication sharedApplication]keyWindow]];
[actionsheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *string;
string = [NSString stringWithFormat:@"%@",[currentarray objectAtIndex:row]];
return string;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
return [currentarray count];
}