无法在同一个Controller中使用两个UIPickerView

时间:2014-05-29 04:45:42

标签: ios objective-c uipickerview

我试图在一个ViewController中一起使用两个以上的UIPickerViews。每个UIPickerView都有不同的数据数组。我正在使用界面构建器来链接拾取器。我想要的是,点击按钮后,我的PickerView将显示,用户可以从中选择数据。所选数据将出现在文本字段中。它适用于第一个PickerView,但当我点击第二个PickerView时,该值来自第一个数组,我无法写入条件。这是我的所有代码:

.h文件

 #import <UIKit/UIKit.h>

    @interface Tabbar2ViewController : UIViewController<UITextViewDelegate,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{

        IBOutlet UIPickerView *genderPickerView;
        IBOutlet UIPickerView *pickerViewoption;

        IBOutlet UIButton *BrandPickerBtn;
        IBOutlet UIButton *genderPickerfBtn;

        NSArray *brands;
        NSArray *gender;
        BOOL isCheckedpickerView;


        IBOutlet UILabel *BrandLabelfield;
        IBOutlet UILabel *GenderLabelfield;

    }

    - (IBAction)action:(id)sender;
    - (IBAction)genderAction:(id)sender;

    @property(nonatomic,retain)NSArray *brands;
    @property(nonatomic,retain)NSArray *gender;

    @end

.m文件

#import "ttViewController.h"

@interface ttViewController ()

@end



 @implementation ttViewController
    @synthesize countries,size,brands,gender,activity,condition,color;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];


        isCheckedpickerView=FALSE;
        pickerViewoption.hidden=YES;
        genderPickerView.hidden=YES;
        activityPickerView.hidden=YES;
        conditionPickerView.hidden=YES;
        sizeandquantityPickerView.hidden=YES;


        BrandLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 812, 253, 35)];
        BrandLabelfield.textColor = [UIColor lightGrayColor];
        BrandLabelfield.text=@"Brand";
        BrandLabelfield.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"header_01.png"]];
         BrandLabelfield.font = [UIFont fontWithName:@"Open Sans" size:14];
        [tabView1 addSubview:BrandLabelfield];


        //GenderTextfield with Label //

        GenderLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 865, 253, 35)];
        GenderLabelfield.textColor = [UIColor lightGrayColor];
        GenderLabelfield.text=@"Gender";
        GenderLabelfield.font = [UIFont fontWithName:@"Open Sans" size:14];
        [tabView1 addSubview:GenderLabelfield];

        //ColorTextfield with Label //

        ConditionLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 1113, 253, 35)];
        ConditionLabelfield.textColor = [UIColor lightGrayColor];
        ConditionLabelfield.text=@"Color";
        ConditionLabelfield.font = [UIFont fontWithName:@"Open Sans" size:14];
        [tabView1 addSubview:ConditionLabelfield];
        brands=[[NSArray alloc]initWithObjects:@"Nike",@"Bata",@"Adidas", nil];
        gender=[[NSArray alloc]initWithObjects:@"Male",@"Female",@"Unisex", nil];
        activity=[[NSArray alloc]initWithObjects:@"BASEBALL",@"BASKETBALL",@"FOOTBALL",@"GOLF",@"RUNNING",@"SKATING",@"SOCCER",@"SPORTS & OUTDOOR",@"TENNIS",@"TRAINING",@"VOLLEY BALL",@"WRESTLING", nil];

        // Custom PICKER VIEWS //


        pickerViewoption=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 852, 290, 41)];
        pickerViewoption.showsSelectionIndicator = YES;
        pickerViewoption.delegate = self;
        pickerViewoption.dataSource=self;
        pickerViewoption.hidden=YES;
        [pickerViewoption setBackgroundColor:[UIColor lightGrayColor]];
        [tabView1 addSubview:pickerViewoption];

        genderPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 906, 290, 41)];
        genderPickerView.showsSelectionIndicator = YES;
        genderPickerView.delegate = self;
        genderPickerView.dataSource=self;
        genderPickerView.hidden=YES;
        [genderPickerView setBackgroundColor:[UIColor lightGrayColor]];
        [tabView1 addSubview:genderPickerView];

        activityPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 954, 290, 41)];
        activityPickerView.showsSelectionIndicator = YES;
        activityPickerView.delegate = self;
        activityPickerView.dataSource=self;
        activityPickerView.hidden=YES;
        [activityPickerView setBackgroundColor:[UIColor lightGrayColor]];
        [tabView1 addSubview:activityPickerView];




        pickerViewoption.tag=1;
        genderPickerView.tag=2;
        activityPickerView.tag=3;

        BrandPickerBtn.tag=000;
        genderPickerfBtn.tag=111;
        activityPickerBtn.tag=222;

     }


    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
         }


    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
    forComponent:(NSInteger)component{

        NSString *string;


        if (pickerViewoption.tag==1) {

             [tabView1 addSubview:pickerViewoption];
            string=[self.brands objectAtIndex:row];
        }
        else  {
                [tabView1 addSubview:genderPickerView];
                 string=[self.gender objectAtIndex:row];
              }


        else if (genderPickerView.tag==2){
            [tabView1 addSubview:genderPickerView];
            string=[self.gender objectAtIndex:row];
       }

        else if (activityPickerView.tag==3){
          [tabView1 addSubview:activityPickerView];
           string=[self.activity objectAtIndex:row];
       }




     return string;

          }


    - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
           inComponent:(NSInteger)component {


        if (pickerViewoption.tag==1){
            BrandLabelfield.text=@"";
             BrandLabelfield.text=[brands objectAtIndex:row];
            BrandLabelfield.textColor=[UIColor whiteColor];
            pickerViewoption.hidden=YES;

        }
        else if (genderPickerView.tag==2){
             GenderLabelfield.text=@"";
             GenderLabelfield.text=[gender objectAtIndex:row];
            GenderLabelfield.textColor=[UIColor whiteColor];}

        else if (activityPickerView.tag==3){
            ActivityLabelfield.text=@"";
            ActivityLabelfield.text=[activity objectAtIndex:row];
            ActivityLabelfield.textColor=[UIColor whiteColor];
    }

     }



    - (NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component{


        if (pickerViewoption.tag==1)
        {
            return [brands count];
        }
        else if (genderPickerView.tag==2)
        {
            return [gender count];
        }
        else if (activityPickerView.tag==3)
        {
            return [activity count];
        }


        return 0;
    }




    - (IBAction)action:(id)sender; {
          if (isCheckedpickerView==FALSE)
        {
            pickerViewoption.hidden=NO;
            isCheckedpickerView=TRUE;

        }
        else
        {
            isCheckedpickerView=FALSE;
            pickerViewoption.hidden=YES;
         }

    }




    - (IBAction)genderAction:(id)sender {


        if (isCheckedpickerView==FALSE)
        {
             genderPickerView.hidden=NO;
             isCheckedpickerView=TRUE;
        }
        else
        {
            isCheckedpickerView=FALSE;
            genderPickerView.hidden=YES;

        }


         }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{
        [textField resignFirstResponder];

        return YES;
    }



    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.
    }

    @end

1 个答案:

答案 0 :(得分:5)

目前您正在检查委托方法中的错误条件,请在.m文件中替换以下3种方法

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
            forComponent:(NSInteger)component{

      NSString *string;

      if (pickerView.tag==1) {
           [tabView1 addSubview:pickerViewoption];
           string=[self.brands objectAtIndex:row];
                }

      else if (pickerView.tag==2){
            [tabView1 addSubview:genderPickerView];
            string=[self.gender objectAtIndex:row];
               }

      else if (pickerView.tag==3){
            [tabView1 addSubview:activityPickerView];
            string=[self.activity objectAtIndex:row];
               }

      else  {
            [tabView1 addSubview:genderPickerView];
            string=[self.gender objectAtIndex:row];
                      }

      return string;

}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
               inComponent:(NSInteger)component {

   if (thePickerView.tag==1){
          BrandLabelfield.text=@"";
          BrandLabelfield.text=[brands objectAtIndex:row];
          BrandLabelfield.textColor=[UIColor whiteColor];
          pickerViewoption.hidden=YES;
        }
    else if (thePickerView.tag==2){
          GenderLabelfield.text=@"";
          GenderLabelfield.text=[gender objectAtIndex:row];
          GenderLabelfield.textColor=[UIColor whiteColor];
        }

    else if (thePickerView.tag==3){
          ActivityLabelfield.text=@"";
          ActivityLabelfield.text=[activity objectAtIndex:row];
          ActivityLabelfield.textColor=[UIColor whiteColor];
        }

}

- (NSInteger)pickerView:(UIPickerView *)pickerView
        numberOfRowsInComponent:(NSInteger)component{

            if (pickerView.tag==1)
            {
                return [brands count];
            }
            else if (pickerView.tag==2)
            {
                return [gender count];
            }
            else if (pickerView.tag==3)
            {
                return [activity count];
            }


    return 0;
}