无法在同一个控制器中使用两个 UIPickerView

Posted

技术标签:

【中文标题】无法在同一个控制器中使用两个 UIPickerView【英文标题】:Not able to use two UIPickerView in the same Controller 【发布时间】:2014-05-29 04:45:41 【问题描述】:

我试图在一个 ViewController 中同时使用两个以上的 UIPickerView。每个 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

【问题讨论】:

旁注 - 为什么每次请求选择器视图的标题时,您会一遍又一遍地将每个选择器添加为子视图?这简直是​​愚蠢的。 旁注2:如果您使用IB创建选择器视图并将它们分配给出口,为什么还要在代码中创建选择器视图(替换在IB中创建的)? @rmaddy 是的,它被分配了已经在 IB 中分配的选择器。 【参考方案1】:

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

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

【讨论】:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 我需要改变什么 检查该方法中的 if else 条件 这里的pickerView是什么。在可能的情况下。 pickerview 名称(第一个 pickerview , secondpickerview 等)@iPhone Dev pickerView 是一个本地方法对象。 我没听懂你在说什么

以上是关于无法在同一个控制器中使用两个 UIPickerView的主要内容,如果未能解决你的问题,请参考以下文章

根据数据显示数据 UIPickerView

在 iOS 7 中重用 UIPickerView 中的视图

无法关闭嵌入在导航控制器中的两个视图控制器

可以更改 UIPickerView 的全局文本颜色吗?

无法在控制器中自动装配多个服务

Thymeleaf - 我无法使用 Link th:href 将两个值从 HTML 传递到控制器