DatePicker does not show on input click on iOS 8 - ios

I am showing pickerView on input field click it worked fine on other iOS but when I updated to iOS 8 it did not work. When i click on input field it does not show up the date picker.
Here is the code I am using.
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{
appDelegate.recentAddSowID = #"";
if (aTextField == dateTextField) {
[dateTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"" destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode=UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerView addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[dateTextField setInputView:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
}
I am testing on iPhone 5S.

They changed the implementation of UIActionSheet in iOS 8. You can no longer add views to its view hierarchy
The documentation (https://developer.apple.com/library/ios/documentation/uikit/Reference/UIActionSheet_Class/index.html) states:
UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.
Also, the UIActionSheet is deprecated in iOS 8
Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet.
As an alternative, try this: https://github.com/skywinder/ActionSheetPicker-3.0. It works with iOS 8 and might be what you need
Source: Add UIPickerView in UIActionSheet from IOS 8 not working

Related

UIPickerView Only Responds Within Area of Tab Bar

I'm updating my app to iOS 8 and trying to recreate the UIPickerView inside a UIActionSheet with UIAlertController as described here. Using the answer from Nada Gamal, I was able to recreate the functionality, but the UIPickerView only responds within the tab bar (even though the tab bar is hidden behind the picker view). The tab bar controller is the root view controller.
-(void)loadPickerView{
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.backgroundColor = [UIColor whiteColor];
[pickerView selectRow:pickerViewRow inComponent:0 animated:NO];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
pickerToolbar.tintColor = self.navigationController.navigationBar.tintColor;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
UIAlertController * searchActionSheet=[UIAlertController alertControllerWithTitle:#"" message:#"" preferredStyle:UIAlertControllerStyleActionSheet];
[ searchActionSheet.view setBounds:CGRectMake(8, 208, self.view.frame.size.width, pickerView.frame.size.height+pickerToolbar.frame.size.height)];
[searchActionSheet.view addSubview:pickerToolbar];
[searchActionSheet.view addSubview:pickerView];
[self presentViewController:searchActionSheet animated:YES completion:nil];
}
The presentViewController in the last line seems to be the issue. I've tried many different ways to get the pickerView to respond outside the tab bar area, including:
self
self.tabBarController
self.navigationController (pickerView doesn't show up at all)
[[UIApplication sharedApplication] keyWindow].rootViewController
The other option is to make a separate view controller for this, but I'm concerned that won't work either because UIAlertController is a subclass of UIViewController, but perhaps this is an issue with adding subviews to UIAlertController.

How to modify a Done button programmatically from UIActionSheet?

I have this method to create an action sheet and uipicker:
-(void)presentNewPicker{
self.aac = [[UIActionSheet alloc] initWithTitle:#"What City?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"OK", nil];
self.pickrView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
self.pickrView.showsSelectionIndicator = YES;
self.pickrView.dataSource = self;
self.pickrView.delegate = self;
self.cityNames = [[NSArray alloc] initWithObjects: #"Phoenix", #"Tahoe", #"Nevada", #"Lime", #"Florida", nil];
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet:)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[self.aac addSubview:pickerDateToolbar];
[self.aac addSubview:self.pickrView];
[self.aac showInView:self.view];
[self.aac setBounds:CGRectMake(0,0,320, 464)];
}
The button that appears reads DONE but I want to modify it to read, Ready. How do I accomplish this?
Replace:
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet:)];
with:
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Ready" style: UIBarButtonItemStyleBordered target:self action:#selector(dismissActionSheet:)];
You may want to use UIBarButtonItemStyleDone for the style.
I don't know what you want to do, but you might be interested in using the inputView and inputAccessoryView for one of your uiviews, to replace the keyboard. That'll give you the animations for free, and a place to put a toolbar. Some answers that might help: https://stackoverflow.com/a/6075062/220820
https://stackoverflow.com/a/8583703/220820

Custom UIActionSheet squished on iPad

I have read through several similar questions here on stack but so far none of the answers seem to fit. Therefore it must be something so blatantly obvious that I will have to flog myself later once it gets answered.
The code works fine on iPhone but not iPad, the action sheet has little to no height. There is no tool bar and it is fired off a normal UIButton. I have considered a popover however this is a universal app so I would rather not re-wright the same code twice for both iPhone and iPad and try and keep the app as light as possible.
I have tried showFromRect and showFromTabbar.
Thanks
Code:
-(IBAction) setDateTime:(id) sender {
dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *appointmentDayPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[appointmentDayPicker setDatePickerMode:UIDatePickerModeDateAndTime];
[dateSheet addSubview:appointmentDayPicker];
[appointmentDayPicker release];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"Set" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateSet)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelDateSet)];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton,setButton, nil] animated:NO];
[spacer release];
[setButton release];
[cancelButton release];
[dateSheet addSubview:controlToolBar];
[controlToolBar release];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[dateSheet showFromRect:CGRectMake(100,100, 320, 485) inView:self.view animated:YES];
[dateSheet setBounds:CGRectMake(0, 0, 200, 485)];
} else {
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
}
EDIT: Adding pic

ios 6: UIDatePicker in the UITextField

I have problems with connecting datepicker with text field. I have tried all solution from stackoverflow but they don't work in ios 6. Can you desribe how to make it, I want to tap on text field and choose date via datepicker.
You should use the inputView property of your UITextField to make the picker show in place of the keyboard. iOS6 will take care of everything for you then (showing the picker with the animation instead of the keyboard, etc).
Note: You will probably want to add some inputAccessoryView too (generally an UIToolBar with some "OK" button in it) to make the user be able to dismiss the picker (the IBAction of your "OK" button will simply call [textField resignFirstResponder] for that to happen of course) as an UIDatePicker does not have any button to validate your input (whereas the keyboard has its "Return Key")
#import "TextfieldwithDatepickerViewController.h"
UIActionSheet *pickerViewPopup;
#implementation TextfieldwithDatepickerViewController
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
-(void)doneButtonPressed:(id)sender{
//Do something here here with the value selected using [pickerView date] to get that value
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}

Pickerview for TextField

I have this code, it's inside an action that it's triggered when the user selects the textfield to show the datepicker:
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showFromTabBar:self.tabBarController.tabBar];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 500)];
I was using this code in an app with a tab bar for navigation. Now I would like to use it in an app without it, but when the process reaches [pickerViewPopup showFromTabBar:self.tabBarController.tabBar]; it crashes.
Any idea on how I could fix it?
Use one of the following,
– showFromToolbar:
– showInView:
– showFromBarButtonItem:animated:
– showFromRect:inView:animated:
For eg:-
[pickerViewPopup showFromToolbar:pickerToolbar];
The problem was that you dont have a tab bar and you are trying to access the tab bar object to show the picker from it. Since tabbar is nil for you, it will crash.
For more details check the apple documentation.

Resources