Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with UITextFields inside of UIActionsheet?

i am new in iphone.i am doing reminder application to my project . i want get input values like reminder title and data&Time from uiactionsheet.i have created datepicker and uitextfield in actionsheet . The actionsheet appears with textfield.when i clicked on textfield,the keypad appears but not working means..when i was typed they are not visible in the textfield

-(void)btnSetRemainderTapped:(id)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Select reminder date and time"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:@"Save"
                                         otherButtonTitles:nil,nil];
 menu.tag =10;
[menu showInView:self.view];
menu.userInteractionEnabled=YES;

UITextField  *txtReminderTitle= [[UITextField alloc] initWithFrame:CGRectMake(20.0, 180, 280.0, 40.0)];
[txtReminderTitle setTag:99];
[txtReminderTitle setBackgroundColor:[UIColor whiteColor]];
[txtReminderTitle setFont:[UIFont boldSystemFontOfSize:17]];
[txtReminderTitle setBorderStyle:UITextBorderStyleNone];
[txtReminderTitle setTextAlignment:UITextAlignmentCenter];
txtReminderTitle.borderStyle = UITextBorderStyleRoundedRect;
txtReminderTitle.keyboardType = UIKeyboardTypeDefault;
txtReminderTitle.returnKeyType = UIReturnKeyDone;
txtReminderTitle.delegate =self;
[menu addSubview:txtReminderTitle];

UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.tag =9999;
[menu addSubview:pickerView];

CGRect menuRect = menu.frame;
NSInteger orgHeight = menuRect.size.height;
menuRect.origin.y -= 270; //height of picker
menuRect.size.height = orgHeight+270;
menu.frame = menuRect;

CGRect pickerRect = CGRectMake(20, 250, 280, 200);
pickerView.frame = pickerRect;

}

-(void) actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
  {
UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:9999];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterLongStyle;
[df setTimeStyle:NSDateFormatterShortStyle];
UITextField * txtReminderTitle = (UITextField *)[actionSheet viewWithTag:99];
   NSLog(@"%@",txtReminderTitle.text);  //it print null value
  }

I'm not sure why it's not accepting input from the keyboard. Any suggestions?

like image 463
Ravindhiran Avatar asked Feb 12 '13 04:02

Ravindhiran


1 Answers

I found that, when you add UITextField to UIActionSheet then UITextField is remain under the UIActionSheet. So you need to bring UITextField above UIActionSheet thats way
[menu addSubview:txtReminderTitle]; does not work for UITextField to input any text.

Use following code for add UITextField:

UIWindow *appWindow = [UIApplication sharedApplication].keyWindow;
[appWindow insertSubview:txtReminderTitle aboveSubview:menu];

Only need to do changes in how to add UITextField.

like image 74
iPatel Avatar answered Oct 04 '22 15:10

iPatel