user1745033
user1745033

Reputation:

Run ibaction when UIPickerView item is selected

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

Answers (3)

RayofHope
RayofHope

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

AppleDelegate
AppleDelegate

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

deleted_user
deleted_user

Reputation: 3805

objectAtIndex:0     

obj c uses zero based arrays

Upvotes: 1

Related Questions