How to work with UITextFields inside of UIActionsheet?
Asked Answered
M

2

6

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?

Muffle answered 12/2, 2013 at 4:51 Comment(7)
This is an abuse of UIActionSheet. This is not it's purpose. Do not use standard UI components in a way that they are not designed. What happens when Apple completely changes how a UIActionSheet works or looks in a future version of iOS? Create your own custom view for displaying what you need.Enow
but wt u need to put UITextField in UIActionSheet ?? it very simple and pretty to put it in UIAlertView.Puissant
why you not use Alert instead of UIActionsheet..?Dwt
@Puissant He also needs a UIDatePicker. You can't put that in a UIAlertView.Enow
i have checked alertView and ViewController working fine but my doubt is can we implement textfield inside uiactionsheet?Muffle
@Muffle A UIActionSheet is only for displaying a set of choices (buttons). It is not a general purpose control for showing arbitrary content. Use it only for its intended purpose. You can create your own custom view that slides up from the bottom like an action sheet if you wish. UIAlertView does support 1 or 2 text fields but no other custom content such as a date picker.Enow
possible duplicate of how to enable text input in UITextField which is in UIActionSheet?Enow
P
4

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.

Puissant answered 6/3, 2013 at 12:43 Comment(0)
G
0

Use custom UIActionSheet . do like following
1) add new UIViewController
2) declare one UITextField

@interface ViewController  
{
            UITextField *TextField;

    }

3) add delegates of ActionSheet & TextField

@interface ViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate> {

        UITextField *TextField;

    }

4)Now implement methods

see this link just i see on stackoverflow

Gonfanon answered 12/2, 2013 at 5:12 Comment(3)
@Muffle The accepted answer in the posted link is what I am talking about. It is a custom class that does what you want without using a UIActionSheet. Please consider using that code as a basis for your solution.Enow
k thanks,small doubt you posted link why he implemented @interface trialViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate> what is the need for UIActionsheetDelegate? he used instead of uiviewMuffle
@Muffle There is no reason at all that the custom view controller should adhere to UIActionSheetDelegate. That should be removed.Enow

© 2022 - 2024 — McMap. All rights reserved.