У меня уже есть три выхода TextField, подключенных к моему Nib-файлу
IBOutlet UITextField *myTextField;
IBOutlet UITextField *myTextFieldd;
IBOutlet UITextField *myTextFields;
Теперь я пытаюсь добавить средство выбора для всплывающего окна при выборе myTextFieldd и myTextFields, обратите внимание, что myTextField прекрасно работает.Я также пытаюсь использовать один и тот же сборщик для POP Up для трех текстовых полей.
Код:
#import "PickrAppViewController.h"
@implementation PickrAppViewController
@synthesize categoryArray,selectedCategory;
- (void)viewDidLoad {
[super viewDidLoad];
categoryArray = [[NSMutableArray alloc] initWithObjects:@"Jack",@"Jone",nil];
}
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == myTextField)
{
[numberTextField resignFirstResponder];
if([myTextField.text isEqualToString:@""]){
myTextField.text = [self.categoryArray objectAtIndex:0];
}
NSInteger pickerRow;
for(NSInteger i = 0; i < [self.categoryArray count]; i++){
NSString *string = [self.categoryArray objectAtIndex:i];
if([string isEqualToString:myTextField.text]){
pickerRow = i;
break; //Once we have it break out of the loop
}
}
[picker selectRow:pickerRow inComponent:0 animated:NO];
pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.50];
[UIView setAnimationDelegate:self];
pickrView.frame = CGRectMake(0, 300, pickrView.frame.size.width, pickrView.frame.size.height);
[self.view addSubview:pickrView];
[UIView commitAnimations];
return NO;
}
if([pickrView superview]){
[self animationForPickrDown];
}
return YES;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([pickrView superview]){
[self animationForPickrDown];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark
#pragma mark PickrView datasource methods
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.selectedCategory = [NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:row]];
myTextField.text = self.selectedCategory;
[self animationForPickrDown];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.categoryArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.categoryArray objectAtIndex:row];
}
- (void) animationForPickrDown
{
[UIView beginAnimations:nil context:NULL];
pickrView.frame = CGRectMake(0, 300, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView setAnimationDuration:.50];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stopAnimation)];
pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView commitAnimations];
}
- (void) stopAnimation
{
if([pickrView superview]){
[pickrView removeFromSuperview];
}
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[myTextField release];
[pickrView release];
[selectedCategory release];
}
@end
Спасибо