I have a UITextField, in which textfield action is not working when UITextField is a subview of UIScrollView.
Other wise , it is working fine , Here is my code .. Please Help ?
I am using this code because i need to fire a UIDatePicker Right after alertview Buttons click.
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
date = [[UITextField alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
date.placeholder = @"Date";
date.delegate = self;
[date addTarget:self action:@selector(dateClicked:) forControlEvents:UIControlEventTouchDown];
[scroll addSubview:date];
}
- (void)dateClicked:(id)sender
{
NSLog(@"CLICKED");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Choose Date Format" message:nil delegate:self cancelButtonTitle:@"dd/MM/YYYY" otherButtonTitles:@"MM/dd/YYYY", nil];
[alertView show];
}
you set UIButton
same frame as UITextField
and set UIButton
UIControlEventTouchUpInside
event and update UITextField
value.
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
date = [[UITextField alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
date.placeholder = @"Date";
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
[btn addTarget:self action:@selector(dateClicked:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:date];
[scroll addSubview:btn];
}
- (void)dateClicked:(id)sender
{
NSLog(@"CLICKED");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Choose Date Format" message:nil delegate:self cancelButtonTitle:@"dd/MM/YYYY" otherButtonTitles:@"MM/dd/YYYY", nil];
[alertView show];
}
i have created simple Example as per your requirement look at this:
in .h file
UITextField *date;
NSMutableArray *arrData;
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
date = [[UITextField alloc]initWithFrame:CGRectMake(80, 150, 150, 50)];
date.placeholder = @"Date";
date.delegate = self;
[date addTarget:self action:@selector(dateClicked:) forControlEvents:UIControlEventEditingDidBegin];
[scroll addSubview:date];
arrData = [NSMutableArray array];
for(int i=0;i<10;i++)
{
[arrData addObject:[NSString stringWithFormat:@"Object - %d",i]]
}
}
- (void)dateClicked:(id)sender
{
NSLog(@"CLICKED");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Choose Date Format" message:nil delegate:self cancelButtonTitle:@"dd/MM/YYYY" otherButtonTitles:@"MM/dd/YYYY", nil];
[alertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(80, 200, 150, 150)];
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 5;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return arrData[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
date.text = arrData[row];
[pickerView removeFromSuperview];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.