How to work with UITextFields inside of UIActionsheet? - iphone

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?

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.

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

Related

Invalid context error on iOS 7 when adding a UIPickerView inside a UIActionSheet?

I have an UIPickerView inside an UIActionSheet and have done that in a way suggested by many others here at SO:
Add UIPickerView & a Button in Action sheet - How?
how to add UIPickerView in UIActionSheet
The solutions have worked fine until now when testing my app on iOS 7. It still works but I got a lot of "invalid context 0x0" warnings during runtime in Xcode.
The errors are coming with a nice message too:
CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Could it be that Apple finally want to stop this kind of solution or is it something we can work around or fix?
A workaround for this 'invalid context 0x0' warning is to init the UIActionSheet with a non nil title (non-nil buttons also resolve the error but result in visual artefacts).
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
You will then need to adjust the actionsheet's frame for it to position properly in both iOS7 and previous versions (make sure to do this after the showInView method).
CGRect newFrame = actionSheet.frame;
newFrame = self.view.bounds.size.height - myCustomPicker.frame.size.height;
newFrame = myCustomPicker.frame.size.height;
actionSheet.frame = newFrame;
As the comments above point out, the UIActionSheet is not designed to be subclassed or take other views.
More info here at Apple:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006801-CH3-DontLinkElementID_2
One approach is to look closer at the DateCell sample code and change that to use an UIPickerView instead:
https://developer.apple.com/library/ios/samplecode/DateCell/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008866-Intro-DontLinkElementID_2
-(void)viewDidLoad
{
[super viewDidLoad];
// Make Your own Action sheet
myCustomeActionSheet=[[UIView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height+1,self.view.frame.size.width,215)];
myCustomeActionSheet.backgroundColor=[UIColor whiteColor];
[self.view addSubview:myCustomeActionSheet];
}
// Use this code To Open Date Picker Or UiPicker View
-(void)OpenActionSheet:(Boolean)isDatePickere
{
SelectedString=Nil;
if (datepickerView) {
[datepickerView removeFromSuperview];
datepickerView=Nil;
}
if (picker) {
[picker removeFromSuperview];
picker=Nil;
}
if (isDatePickere)
{
// Add the Datepicker
datepickerView= [[UIDatePicker alloc] init];
datepickerView.datePickerMode = UIDatePickerModeDateAndTime;
datepickerView.minimumDate=[NSDate date];
[myCustomeActionSheet addSubview:datepickerView];
}
else
{
// Add Picker View
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,40, 320, 216)];
picker.showsSelectionIndicator=YES;
picker.dataSource = self;
picker.delegate = self;
[myCustomeActionSheet addSubview:picker];
}
UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,320,40)];
tools.barStyle=UIBarStyleBlackOpaque;
[myCustomeActionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinCancelClicked)];
UIBarButtonItem *flexSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [[NSArray alloc]initWithObjects:CancelButton,flexSpace,flexSpace,doneButton,nil];
[tools setItems:array];
//picker title
UILabel *lblPickerTitle=[[UILabel alloc]initWithFrame:CGRectMake(60,8, 200, 25)];
lblPickerTitle.text=#"Select";
lblPickerTitle.backgroundColor=[UIColor clearColor];
lblPickerTitle.textColor=[UIColor whiteColor];
lblPickerTitle.textAlignment=NSTextAlignmentCenter;
lblPickerTitle.font=[UIFont boldSystemFontOfSize:15];
[tools addSubview:lblPickerTitle];
[UIView animateWithDuration:0.5 animations:^{
myCustomeActionSheet.frame=CGRectMake(0,self.view.frame.size.height-myCustomeActionSheet.frame.size.height,myCustomeActionSheet.frame.size.width,myCustomeActionSheet.frame.size.height);
} completion:^(BOOL finished)
{
}];
}
pragma -mark Bar Button Action
-(void)btnActinDoneClicked
{
if (SelectedString==Nil)
{
SelectedString=[pickerArrayList objectAtIndex:0];
}
if (datepickerView)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:posix];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"DD/MM/yy hh:mm a"];
[selectedButton setTitle:[dateFormatter stringFromDate:datepickerView.date] forState:UIControlStateNormal];
}
else
[selectedButton setTitle:SelectedString forState:UIControlStateNormal];
[self performSelector:#selector(btnActinCancelClicked) withObject:nil afterDelay:0.10];
}
-(void)btnActinCancelClicked
{
[UIView animateWithDuration:0.5 animations:^{
viewActiobSheetView.frame=CGRectMake(0,self.view.frame.size.height+1,self.view.frame.size.width,viewActiobSheetView.frame.size.height);
}
completion:^(BOOL finished)
{
[datepickerView removeFromSuperview];
[picker removeFromSuperview];
datepickerView=Nil;
picker=Nil;
}];
}
pragma -mark PickerView Delegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view
{
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
label.text=[pickerArrayList objectAtIndex:row];
label.textAlignment=NSTextAlignmentCenter;
label.textColor=[UIColor blackColor];
return label;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerArrayList count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
SelectedString=[pickerArrayList objectAtIndex:row];
}

Make a date picker appear when a user taps a text field

Im trying to follow this post Display datepicker on tapping on textfield and when I get to the point where it says "In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach it to the view" i get lost. Im using storyboards and he's using xib's. Is that the problem? xib's aren't an option for me. So is there a way to drag a date picker out, but not attach to the view?
The difference with storyboard is that you would use a XIB file (also called NIB) for each view.
But you should be able to follow any instructions.
In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach
it to the view.
I believe he means not to connect to the ViewController like you would do with a button (IBAction for example)
Since you are showing when you tap the textfield then you have to do it programmatically.
You don't need to add it to your view really.
You can create a viewcontroller that has a date picker at the bottom and a close button at the top.
When tapping the textfield you can just present that viewcontroller modally.
Set delegate methods to the viewcontroller for:
closeButtonClicked: , and dateSelected: ,
and implemet these delegate merhod in the main controller.
In addition, you can "block" the screen by adding the datePicker's viewcontroller background color with certain alpha for a nice affect.
I found that to be the easiest way to do that. No need for a nib
May be it will helpful to you..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
dateActionSheet = [[UIActionSheet alloc]initWithTitle:#"Select Your Date of Birth" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil , nil];
dateActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged ];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[dateActionSheet addSubview:datePicker];
[dateActionSheet addSubview:self.datePickerView];
UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,320,40)];
tools.barStyle=UIBarStyleBlackOpaque;
[dateActionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *flexSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [[NSArray alloc]initWithObjects:flexSpace,flexSpace,doneButton,nil];
[tools setItems:array];
//picker title
UILabel *lblPickerTitle=[[UILabel alloc]initWithFrame:CGRectMake(60,8, 200, 25)];
lblPickerTitle.text=#"Select Date of Birth";
lblPickerTitle.backgroundColor=[UIColor clearColor];
lblPickerTitle.textColor=[UIColor whiteColor];
lblPickerTitle.textAlignment = NSTextAlignmentCenter;
lblPickerTitle.font=[UIFont boldSystemFontOfSize:15];
[tools addSubview:lblPickerTitle];
//cell.detailTextLabel.text = [self datePickerValueChangedd:datePicker];
[dateActionSheet showFromRect:CGRectMake(0,480, 320,215) inView:self.view animated:YES];
[dateActionSheet setBounds:CGRectMake(0,0, 320, 411)];
}
-(void)btnActinDoneClicked {
[dateActionSheet dismissWithClickedButtonIndex:1 animated:YES];
[dateActionSheet removeFromSuperview];
}
-(void)datePickerValueChangedd:(UIDatePicker*) datePicker
{
Youytextfield.text = (NSString *)datePicker.date;
}

UIActionSheet with table view

HEllo,
can any one guide me for the following
I want to add an ActionSheet with customize image.
In ActionSheet I want to place a table view for the data.
Two buttons(cancel & done)
Thanks....
check my answer. I am using this code to display UITableView in actionsheet.
In .h file
#property (strong, nonatomic) IBOutlet UITableView *tableView;
In .m file
-(void)addTableViewInActionSheet
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 320, 210)];
_tableView.dataSource = self;
_tableView.delegate = self;
[actionSheet addSubview:_tableView];
UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
doneButton.segmentedControlStyle = UISegmentedControlStyleBar;
doneButton.tintColor = DEFAULT_COLOR;
[doneButton addTarget:self action:#selector(doneBtnClicked:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];
UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(10, 7.0f, 60.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor blackColor];
[cancelButton addTarget:self action:#selector(cancelBtnClicked:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:cancelButton];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
You don't need to added table in UIActionSheet just add 7 - 8 buttons in UIActionSheet and it will automatically be placed as table.
See the attached screenshot..

iPhone fill values for UIPickerView

I've created a UIPickerView, which displays on button click. The question is how I fill in the rowItems
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Location"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Done", #"")]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
and How do I get value when "done" is clicked.
you've set the delegate of the alert, and the picker already. Now you have to implement them. You don't fill in data for the picker, the picker asks your delegate for the data.
You need the mandatory functions defined in these three protocols.
UIPickerViewDelegate
UIPickerViewDataSource
UIActionSheetDelegate
You have to implement -(IBAction)dismissActionSheet:(UIButton *)sender too.
and How do I get value when "done" is clicked.
I would store the value to a instance variable every time the picker changes, and once the done button is clicked I would assign it to my data model.
Edit: If you want to learn something I would get rid of the copy&paste solution and try to implement it on my own. But YMMV.

UIActionSheet doesn't show in view and screen goes dark

I have an app with tabs and navigation controllers.
Everything works great except a UIActionSheet. I even have a UIAlertView taht shows fine in the same controller, but the action sheet doesn't show. The screen goes dark, like it's showing, but no view.
Here's the relevant code:
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Erase the file?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Clear List"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
//[actionSheet showInView:self.view];
//MyAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
//[actionSheet showInView:delegate.tabBarController.view];
[actionSheet release];
The commented out code was the different ways of showing it that I've tried.
Any thoughts as to why this isn't working?
[actionSheet showInView:self.view.window];
Don't show it from the current keyWindow - show it from the actual tab bar with something like
[actionSheet showFromTabBar:delegate.tabBarController.tabBar];
Try, this works perfectly:
UITextField *tempTextField;
UIActionSheet *myActionSheet;
- (IBAction) myDatePickerAction:(id)sender {
tempTextField = (UITextField *)sender;
[(UITextField *)sender resignFirstResponder];
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[myActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIDatePicker *theDatePicker = [[UIDatePicker alloc] init];
theDatePicker.datePickerMode = UIDatePickerModeDate;
CGRect datePickRect = theDatePicker.frame;
datePickRect.size.height = 120;
theDatePicker.frame = datePickRect;
[theDatePicker addTarget:self action:#selector(setDateValue:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:theDatePicker];
[theDatePicker release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
CGRect closeButtonRect = closeButton.frame;
closeButtonRect.size.width = 50;
closeButtonRect.size.height = 30;
closeButton.frame = closeButtonRect;
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showFromTabBar:appDelegate.tabBarController.tabBar];
CGRect actionSheetRect = myActionSheet.bounds;
actionSheetRect.size.height = 300;
myActionSheet.bounds = actionSheetRect;
}
- (IBAction)setDateValue:(id)sender {
NSDateFormatter *myDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSLog(#"Formatted date1:%#",[myDateFormatter stringFromDate:[(UIDatePicker *)sender date]]);
tempTextField.text = [myDateFormatter stringFromDate:[(UIDatePicker *)sender date]];
tempTextField = nil;
}
- (IBAction)dismissActionSheet:(id)sender {
[myActionSheet dismissWithClickedButtonIndex:0 animated:YES];
myActionSheet = nil;
//[myActionSheet release];
//Do not release here. Just set to nil and release it in dealloc.
}