2011-04-16 51 views
2

我尝试在用户单击UITextField时使UIPickerView显示出来,我应该看到数据源和委托出口将它们与选取器相关联,但是,它当我打开nib文件时不退出 - >点击文件所有者 - >检查员 第二个问题是当我点击UItextField时键盘没有隐藏,虽然我做了一个textFieldShouldReturn方法,它假设隐藏键盘。UIPickerview无法在界面生成器中找到委托和数据源插座

我在这里错过了什么?

.h文件:

@interface RechercherViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate> { 

     IBOutlet UIPickerView *pickerTypesCarburants; 
     IBOutlet UIView  *pickerViewTypesCarburants; 
     NSMutableArray  *typesCarburantsArray; 
     IBOutlet UITextField *typeCarburantTextField; 
    } 
    -(IBAction)pickerTypeCarburantsShow; 
    -(IBAction)pickerTypeCarburantsDone; 
    @end 

.m文件:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{ 

     return 1; 

    } 

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

     return [typesCarburantsArray count]; 

    } 

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


     return [typesCarburantsArray objectAtIndex:row];  



    } 

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

     UITouch *touch; 
     touch=[touches anyObject]; 
     CGPoint point=[touch locationInView:self.view]; 
     if(CGRectContainsPoint([typeCarburantTextField frame],point)) 
     { 
      [self pickerTypeCarburantsShow]; 

     } 

    } 
-(IBAction)pickerTypeCarburantsDone{ 


    NSInteger selectedRow=[pickerTypesCarburants selectedRowInComponent:0]; 
    NSString *item=[typesCarburantsArray objectAtIndex:selectedRow]; 
    typeCarburantTextField.text=[NSString stringWithFormat:@"%@",item]; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 480); 
    pickerViewTypesCarburants.transform=transform; 
    [UIView commitAnimations]; 


} 

-(IBAction)pickerTypeCarburantsShow{ 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 240); 
    pickerViewTypesCarburants.transform=transform; 
    [self.view addSubview:pickerViewTypesCarburants]; 
    [UIView commitAnimations]; 

} 
-(BOOL)textFieldShouldReturn:(UITextField *)textField{ 

    [textField resignFirstResponder]; 
    return YES; 

} 

回答

2

首先,你可能不应该试图使一个UIPickerView出现在你的时候点击一个UITextField,因为它不是标准的行为(特别是如果你压制键盘)。这听起来像你需要一个标准的UIButton,当按下时提供UIPickerView,因为这会更有意义。无论如何,如果您没有看到IB中的数据源和代理插座,请尝试在您的代码中手动应用它们。

pickerTypesCarburants.delegate = self; 
pickerTypesCarburants.dataSource = self; 

要回答你的第二个问题,当你按你已经实现了textFieldShouldReturn方法的返回键键盘只会隐藏。 UITextField也必须有它的委托集(我假设你在IB中这样做,因为没有在你的.m文件中列出)。尽快按下的UITextField使键盘隐藏,你会改变你的pickerTypeCarburantsShow方法:

-(IBAction)pickerTypeCarburantsShow{ 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.5]; 
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 240); 
pickerViewTypesCarburants.transform=transform; 
[self.view addSubview:pickerViewTypesCarburants]; 
[UIView commitAnimations]; 
[typeCarburantTextField resignFirstResponder]; 
} 

这将确保键盘会立即隐藏(而不是在按下回车键)。再次,我会质疑为什么你想在UITextField出现时使UIPickerView出现,因为它可能违背了人机界面指南。

+0

嗨,thx为您的答案,但我确实喜欢你说,没有什么改变:(和你的问题,我会说我的应用程序的要求迫使我这样做 – Malloc 2011-04-16 17:28:04

+0

嗨,你可以尝试为UITextField实现' - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField'委托并返回NO - 这应该防止textField变得可编辑,从而防止键盘出现在第一位置。或者,尝试设置UserInteractionEnabled物业给NO。 – 2011-04-16 23:03:37

相关问题