Reputation:
I'm working in Xcode 4 and have some issues with my picker view. I wan't to go to an other view when the selected object of the UIPickerView equals "European Sights"
This is the action that i want to run:
-(IBAction)switchviewMainToQuiz:(id)sender {
if([arrayPickerView objectAtIndex:1] == @"European Sights"){
ViewControllerQuiz *quizView = [[ViewControllerQuiz alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:quizView animated:YES];
}
else{
lblPickerView.text = @"werkt niet";
}
}
This is the rest of the code (pickerview code):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Put strings in the PickerView and show in label what's selected
arrayPickerView = [[NSMutableArray alloc] init];
[arrayPickerView addObject:@"European Sights"];
[arrayPickerView addObject:@"Flags of the world"];
[arrayPickerView addObject:@"Something2"];
[arrayPickerView addObject:@"Something3"];
[arrayPickerView addObject:@"Something4"];
[pickerView selectRow:1 inComponent:0 animated:NO];
lblPickerView.text= [arrayPickerView objectAtIndex:[pickerView selectedRowInComponent:0]];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
lblPickerView.text= [arrayPickerView objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arrayPickerView count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arrayPickerView objectAtIndex:row];
}
The UIPickerView works and shows the selected item in the label. Someone can help me out? Thank You
Upvotes: 0
Views: 570
Reputation: 1187
Try this lines of code :
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { lblPickerView.text= [arrayPickerView objectAtIndex:row]; if(lblPickerView.text isEqualToString:@"European Sights"){ ViewControllerQuiz *quizView = [[ViewControllerQuiz alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:quizView animated:YES]; } else{ lblPickerView.text = @"werkt niet"; } }
Upvotes: 0
Reputation: 4249
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
lblPickerView.text= [arrayPickerView objectAtIndex:row];
UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
sender.tag = row;
[self switchviewMainToQuiz:(id)sender]; // send a dummy button as the action is conformed for an UIButton interception
}
Upvotes: 1