2013-08-29 37 views

回答

0

,最好的辦法是建立在XIB文件單獨UIPickers ,每一個,關鍵是把它放在一個UIView中,並將該字段的輸入附件視圖設置爲該視圖。

3場需要三個採摘各自的UIView的容器。

0

需要3個文本框,並分別給它們標記爲1,2,3。並設置它的委託。 請不要在.h文件中以下

UIPickerView *packerView1; 
    UIPickerView *packerView2; 
    UIPickerView *packerView3; 
    NSMutableArray *dataArray1; 
    NSMutableArray *dataArray2; 
    NSMutableArray *dataArray3; 

做.m文件以下

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 

    [packerView1 removeFromSuperview]; 
    [packerView2 removeFromSuperview]; 
    [packerView3 removeFromSuperview]; 

    if(textField.tag==1) 
    { 
     packerView1=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)]; 
     packerView1.delegate=self; 
     packerView1.dataSource=self; 
     packerView1.tag=1; 
     [self.view addSubview:packerView1]; 
    } 
    else if(textField.tag==2) 
    { 
     packerView2=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)]; 
     packerView2.delegate=self; 
     packerView2.dataSource=self; 
     packerView2.tag=2; 
     [self.view addSubview:packerView2]; 
    } 
    else 
    { 
     packerView3=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)]; 
     packerView3.delegate=self; 
     packerView3.dataSource=self; 
     packerView3.tag=3; 
     [self.view addSubview:packerView3]; 
    } 

    return YES; 
} 

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

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

// returns the # of rows in each component.. 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if(pickerView.tag==1) 
    { 
     return [dataArray1 count]; 
    } 
    else if(pickerView.tag==2) 
    { 
     return [dataArray2 count]; 
    } 
    else 
    { 
     return [dataArray3 count]; 
    } 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    if(pickerView.tag==1) 
    { 
     return [dataArray1 objectAtIndex:row]; 
    } 
    else if(pickerView.tag==2) 
    { 
     return [dataArray2 objectAtIndex:row]; 
    } 
    else 
    { 
     return [dataArray3 objectAtIndex:row]; 
    } 
} 

希望這將幫助你

+0

太棒了!我已經知道了,但那不是一個好的解決方案。 –

相關問題