iPad - Showing action sheet with picker view - ipad

I'm adding picker view to action sheet which is working fine in iPhone. But in iPad, i'm not seeing the action sheet. I'm seeing one small rectangle at the middle of the screen in iPad. Here's the code that I wrote:
NSString *actionSheetTitle = #"Select Date of Birth";
UIActionSheet *dateSheet = [[UIActionSheet alloc] initWithTitle:actionSheetTitle
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Done",nil];
dateSheet.delegate = self;
dateSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
dateSheet.destructiveButtonIndex = 0; // make the second button red (destructive)
dateSheet.tag = 100;
//Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,185,0,0)];
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView addTarget:self action:#selector(dateSelectionChanged:) forControlEvents:UIControlEventValueChanged];
[dateSheet addSubview:pickerView];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0,0,320,700)];
self.tempDob = self.dob;
if (self.editPatientDetails)
{
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate* dateSelected = [dtFormatter dateFromString:self.dob];
[dtFormatter release];
[pickerView setDate:dateSelected animated:YES];
}
[pickerView release];
[dateSheet release];

Action sheet is shown as picker view in iPad. Its the expected behavior.

Related

Datepicker crashes on textfield iOS 8

I have used UIDatePicker on the UITextField. I have press UITextField to select the date using datepicker 1st time(App working correct), 2nd Time when I have press again on UITextField to select the new date set on UITextField using UIDatePicker then application is crashes.
I am not using UITableView, I am using simple view that's viewController.
Please Help me...
Thanks
- (void)viewDidLoad
{
[super viewDidLoad];
form=[[NSDateFormatter alloc]init];
[form setDateFormat:#"MM-dd-yyyy"];
datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,0,320, 216);
datePicker.backgroundColor = [UIColor whiteColor];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
BOOL a;
if (textField.tag==10) {
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:popoverContent];
navigationController.delegate=self;
popoverController = [[UIPopoverController alloc]
initWithContentViewController:navigationController];
popoverController.delegate=self;
[popoverController setPopoverContentSize:CGSizeMake(320, 250) animated:NO];
[popoverController presentPopoverFromRect:textField.frame inView:self.addView permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
a=NO;
}
else
{
a=YES;
}
return a;
}
-(void)Result
{
NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
formDay.dateFormat=#"MM-dd-yyyy";
NSString *day = [formDay stringFromDate:[datePicker date]];
self.edit_dob.text = day;
}
Error
*** Assertion failure in -[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:], /SourceCache/UIKit/UIKit-3318.16.14/UITableView.m:9269
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource is not set'
I have share a screenshot. I have select date first time successfully and when I have press to reset new date on that textfield that time app crash.
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
You are repeating these operations every time the textfield is focused. This practice is plain wrong, and is a potential cause of various unexpected behavior like the one you encountered. The fact that it works for the first time makes me think that one of these unnecessarily repeated actions is the culprit. (I'd bet on the addTarget:forControlEvents: method call. This should never be done in any case. I don't even know what happens when you try to add the same target again.)
You should structure your code better to make these method calls only once, instead of multiple times like in the code you've shown (e.g. in viewDidLoad instead of textFieldShouldBeginEditing:) and most probably, the error will go away.
You need to retain the datepicker object i.e.:
#property(nonatomic,strong) UIDatePicker *datePicker;
in the .h file.
Here is updated code:
- (void)viewDidLoad
{
[super viewDidLoad];
form=[[NSDateFormatter alloc]init];
[form setDateFormat:#"MM-dd-yyyy"];
_datePicker=[[UIDatePicker alloc]init];//Date picker
_datePicker.frame=CGRectMake(0,0,320, 216);
_datePicker.backgroundColor = [UIColor whiteColor];
_datePicker.datePickerMode = UIDatePickerModeDate;
[_datePicker setMinuteInterval:5];
[_datePicker setTag:10];
[_datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
BOOL a;
if (textField.tag==10) {
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
[popoverView addSubview:_datePicker];
popoverContent.view = popoverView;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:popoverContent];
navigationController.delegate=self;
UIPopoverController *popoverController = [[UIPopoverController alloc]
initWithContentViewController:navigationController];
popoverController.delegate=self;
[popoverController setPopoverContentSize:CGSizeMake(320, 250) animated:NO];
[popoverController presentPopoverFromRect:textField.frame inView:textField permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
a=NO;
}
else
{
a=YES;
}
return a;
}
-(void)Result
{
NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
formDay.dateFormat=#"MM-dd-yyyy";
NSString *day = [formDay stringFromDate:[_datePicker date]];
_edit_dob.text = day;
}

UIDatePicker issue on UITextField(s)

I am coding a TableViewController with a Section called Information where the user enter a name , a date , a description. My problem is that after i've done all the settings for the NSDate picker on the date's textfield , when i click on the description's textfield instead of keyboard i see the date picker and it stores date in the date's textfield.
.h file
#interface ViewControllerInfo : UITableViewController <UITableViewDelegate, UITextFieldDelegate>{
NSDate *date;
UIActionSheet *dateSheet;
}
#property (weak, nonatomic) IBOutlet UITextField *scadenza;
#property (weak, nonatomic) IBOutlet UITextField *dateTextField;
#property (nonatomic, retain)NSDate *date;
-(void)setDatario;
-(void)dismissDateSet;
-(void)cancelDate;
.m file (pointing only the functions needed):
-(void)setDatario;{
dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:pickerFrame];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:datePicker];
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]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDateSet)];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelDate)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, setButton, cancelBtn, nil] animated:NO];
[dateSheet addSubview:controlToolbar];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)cancelDate{
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)dismissDateSet{
NSArray *listofViews = [dateSheet subviews];
for (UIView *subview in listofViews){
if ([subview isKindOfClass:[UIDatePicker class]]){
self.date = [(UIDatePicker*)subview date];
}
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
[dateTextField setText:[dateFormatter stringFromDate:self.date]];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self setDatario];
return NO;
}
I couldn't understand why is interesting also the description's texfield.
Question is : why the description's textfield is matched with the datepicker ?
Replace your textFieldShouldBeginEditing method like this
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == scadenza) {
return YES;
}
else if(textField == scadenza){
[self setDatario];
return NO;
}
return YES;
}
Even better, put a custom UIButton (invisible) of the same size as the textfield right over the textfield. Set the textfield enabled attribute to NO and then show the picker when the button is tapped. When the user selects a date from the picker, update the text in the textfield. This way you won't have to manage the textfield's delegate. I have used both methods and the button is much cleaner and smoother for the user.

UIActionSheet With PickerView not showing up in iPad

I am using the following code to add an actionsheet with a picker view on it to my app. From the iPhone, it displays perfectly, but when viewing on iPad (Universal app), it shows a very small pickerview with none of the other buttons right on the center of the page.
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Select Chapter"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Select"
otherButtonTitles:nil];
// Add the picker
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,185,0,0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES; // note this is default to NO
[menu addSubview:pickerView];
[menu showFromTabBar:self.tabBarController.tabBar];
[menu setBounds:CGRectMake(0,0,320, 700)];
[pickerView release];
[menu release];

Closing UIActionSheet

I have a UIActionSheet and I have added the following - UIPickerView and 2 BarButtonItems. I am trying to close the action sheet when someone clicks the Cancel Button (it is one of the bar button items).
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0,40,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
tools.barStyle = UIBarStyleBlackOpaque;
[actionSheet 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)]; //this is where the problem is
NSArray *array = [[NSArray alloc]initWithObjects:CancelButton, doneButton, nil];
[tools setItems:array];
[actionSheet showFromTabBar:self.tabBarController.tabBar];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
I have added another method to dismiss the action sheet -
-(IBAction)btnActinCancelClicked:(id)sender{
[sender dismissWithClickedButtonIndex:0 animated:YES];
}
The problem is that the app crashes when the BarButton is clicked. I think the problem is caused where the CancelButton is declared and it is unable to send a message to the btnActinCancelClicked method, but I can't figure out how to fix it.
Thanks
Your method is this;
-(IBAction)btnActinCancelClicked:(id)sender
So you need to add it like this (because it receives a parameter);
#selector(btnActinCancelClicked:)
So for example, if a method had three parameters you'd call it #selector(myMethod:andX:andY:)
Also, to access your UIActionSheet (not entirely sure this will work);
UIActionSheet *acSheet = sender.superView;
//Check if it is the ActionSheet here then dismiss it
[acSheet dismissWithClickedButtonIndex:0 animated:YES];
It's not the sender you want to dismiss. It's the ActionSheet. You'll need a reference for that and change your code for something like:
- (IBAction)btnActinCancelClicked:(id)sender{
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

Is having a UIPickerView in a UIActionSheet acceptable?

Is it acceptable (aka would Apple consider it acceptable) to have a UIPickerView in a UIActionSheet?
Yes, It is totally acceptable. To some degree its even encouraged.
Example code: HERE by Erica Sadun in her book iPhone Developer's cookbook. Chapter 11 Recipe 21
Yes. It is acceptable.
set the frame of the UIPickerView
add the UIPickerView to the actionView
As I remember, actionView will be full screen in this case
UIActionSheet *actionView = [[UIActionSheet alloc] initWith...];
UIPickerView *pickerView = [UIPickerView alloc] init...];
pickerView.frame = CGRect(....);
[actionView addSubview:pickerView];
[pickerView release];
[actionView showInView:theView];
[actionView release];
This should do it :
NSString *title = #"\n\n\n\n\n\n\n\n\n\n\n\n"; // <--- Taken from Erica Sadun´s CookBook
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet setBounds:CGRectMake(0,0,320,485)];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
[actionSheet addSubview:picker];
[actionSheet showFromTabBar:[[self tabBarController] tabBar]];
Please remember that you have to set UIPickerView´s dataSource and delegate .

Resources