选择UIPickerView项时运行ibaction

时间:2012-10-17 10:35:02

标签: iphone objective-c ios xcode cocoa

我正在使用Xcode 4,并且在我的选择器视图中存在一些问题。当UIPickerView的选定对象等于“欧洲景点”时,我不想去其他视图

这是我想要运行的操作:

-(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";
    }
}

这是代码的其余部分(pickerview代码):

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


}

UIPickerView工作并显示标签中的选定项目。有人可以帮帮我吗? 谢谢

3 个答案:

答案 0 :(得分:1)

objectAtIndex:0     

obj c使用基于零的数组

答案 1 :(得分:1)

- (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
}

答案 2 :(得分:0)

尝试以下代码:


    - (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";
        }
    }