UIDatePicker not Displaying correct time - iphone

I am using datePicker to create a timePicker . When i click the textField the picker opens and i have select the time in it, after that selected time will display in that textfield . In my case everything works fine except the time displayed in textfield . It shows the wrong time especially minutes.
This my code for creating picker
timePickerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 260)];
timePickerView.backgroundColor = [UIColor clearColor];
//create toolbar using new
UIToolbar *toolbar1;
toolbar1 = [UIToolbar new];
//toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar1.tintColor = [UIColor colorWithRed:0.627 green:0.627 blue:0.655 alpha:1.000];
//[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.6];
[toolbar1 sizeToFit];
toolbar1.frame = CGRectMake(0, 0, 320, 50);
//Add buttons
UIBarButtonItem *doneButtone1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(dismissPicker:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items1 = [NSArray arrayWithObjects: flexItem1, doneButtone1, nil];
//release buttons
[doneButtone1 release];
[flexItem1 release];
//add array of buttons to toolbar
[toolbar1 setItems:items1 animated:NO];
[timePickerView addSubview:toolbar1];
[toolbar1 release];
UIDatePicker *timePicker = [[UIDatePicker alloc]initWithFrame:CGRectZero];
timePicker.autoresizingMask=UIViewAutoresizingFlexibleWidth;
timePicker.datePickerMode = UIDatePickerModeTime;
timePicker.frame = CGRectMake(0, 50.0f, 320.0, 216);
timePicker.tag = 222;
[timePicker addTarget:self action:#selector(selectTime:) forControlEvents:UIControlEventValueChanged];
[timePickerView addSubview:timePicker];
[timePicker release];
And this is the code to display the time in textfield
UIDatePicker *picker =(UIDatePicker *)sender;
DLog(#"%#",picker);
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
df1.dateStyle = NSDateFormatterMediumStyle;
//if(picker.tag =222) {
[df1 setDateFormat:#"HH:MM"];
self.timeTF.text = [NSString stringWithFormat:#"%#",[df1 stringFromDate:picker.date]];
//timeTF.text=[df1 stringFromDate:[NSDate date]];
[df1 release];
It shows hour hand correctly but minute shows always 5
Whats error in my code . can anyone plz help me find out.

This line is the problem:
[df1 setDateFormat:#"HH:MM"];
MM is month i.e. May = 5
You should use mm.

Related

How to change UINavigationBar's label position (frame)?

I need it aligned to left. Or there is no way and I need to add custom label?
Add it to the left button item:
UIView *mCustView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,20)];
mCustView.backgroundColor = [UIColor redColor];
UILabel *mTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,20)];
mTextLabel.backgroundColor = [UIColor blueColor];
mTextLabel.font = [UIFont systemFontOfSize:20];
mTextLabel.textColor = [UIColor blackColor];
mTextLabel.text = #"Random text tested here, so sit back and enjoy";
[mCustView addSubview:mTextLabel];
[mTextLabel release];
UIBarButtonItem *mCustomBarItem = [[UIBarButtonItem alloc] initWithCustomView:mCustView];
self.navigationItem.leftBarButtonItem = mCustomBarItem;
[mCustView release];
You need to add a custom view to the navigation bar's -titleView
self.navigationItem.titleView = someLabel;
I think you could use UIBarButtonSystemItemFixedSpace which provides a way of adding a fixed padding in a navigation bar:
UIBarButtonItem *fixedSpaceItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:NULL]
autorelease];
fixedSpaceItem.width = 50;
UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:nil
action:NULL]
autorelease];
// vc is some view controller
vc.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:
fixedSpaceItem,
cancelItem,
nil];
This will right indent the "Cancel" button 50 points.
I have found the solution by myself:
float offset = 210.0f;
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, offset, 44.01)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];

Determine if a particular UITextField is selected

I have two UITextFields in one UIViewController. I'm opening a UIDatePicker in both the text fields. I want to update the value of the date in the textfields, but I don't know how to check from which textfield's date picker is open. Can someone please explain how I can determine which text field is selected?
- (IBAction)selectATime:(UIControl *)sender {
[self showActionSheetForTimePicker:sender];
}
- (IBAction)showActionSheetForTimePicker:(id)sender
{
//add the action sheet
actionSheetForiPhone = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
pickerForiPhone = [[UIDatePicker alloc] init];
pickerForiPhone.datePickerMode = UIDatePickerModeTime;
[actionSheetForiPhone addSubview:pickerForiPhone];
[actionSheetForiPhone showInView:self.view];
[actionSheetForiPhone setBounds:CGRectMake(0, 0, 320, 520)];
[pickerForiPhone setBounds:CGRectMake(0, 0, 320, 340)];
//adding toolbar to the action sheet
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 45)];
toolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//adding buttons to the toolbar
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissPicker:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(updateTextField:)];
[barItems addObject:cancelButton];
[barItems addObject:flexSpace];//for adding flexible space between cancel and done button
[barItems addObject:doneButton];
[toolbar setItems:barItems];
[actionSheetForiPhone addSubview:toolbar];
CGRect pickerRect = pickerForiPhone.bounds;
pickerRect.origin.y = -100;
pickerForiPhone.bounds = pickerRect;
[pickerForiPhone release];
[actionSheetForiPhone release];
}
- (void)updateTextField:(id) sender
{
//for updating the value of the textfield
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat: #"hh:mm a"];
NSString *time = [df stringFromDate:self.pickerForiPhone.date];
NSLog(#"Time = %#", time);
//if([element respondsToSelector:#selector(setText:)])
{
if(activeTextField == self.fromTime)
self.fromTime.text = time;
else
self.toTime.text = time;
}
[df release];
//for dismissing the date picker
[self.actionSheetForiPhone dismissWithClickedButtonIndex:1 animated:YES];
}
use tags to identify different textfields.Then u can check if (textfield.tag==something) do something
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
[txtField addTarget:self action:#selector(setPicker:)forControlEvents:UIControlEventEditingDidBegin];
}
-(void)setPicker:(id)sender
{
if ([sender tag] == 1) { //first textField tag
//textField 1
}
else {
//textField 2
}
}

How do i pop up a window when a user taps on the particular row in my application?

How to show a pop up window when the user clicks on the particular field in my table view.
It should pop a window ant it should display the user contact information.
I do not want to use navigationbar controller here.
Kindly help me
I have written code of UIPopOverController with DatePicker as a example for you. just refer this example and set according your need
-(IBAction)tDriveBtnPressed:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
txtDate.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:[NSDate date]]];
[df release];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
CGRect pickerRect = datePicker.bounds;
datePicker.bounds = pickerRect;
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];
datePicker.frame = CGRectMake(0, 44, 320, 300);
[datePicker addTarget:self action:#selector(dateChange:) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:pickerToolbar];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);
//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
CGRect popoverRect = [self.view convertRect:[tDriveBtn frame]
fromView:[tDriveBtn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
popoverRect.origin.x = popoverRect.origin.x;
// popoverRect.size.height = ;
[popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
}
-(void)dateChange:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
txtDate.text= [NSString stringWithFormat:#"%#",
[df stringFromDate:datePicker.date]];
[df release];
}
- (void)pickerDone:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
txtDate.text= [NSString stringWithFormat:#"%#",
[df stringFromDate:datePicker.date]];
[df release];
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
}
You can display your new viewController using presentModalViewController method. I use it like this way:
- (IBAction)addNewBuidling:(id)sender
{
NewBuilding *new=[[NewBuilding alloc]initWithNibName:#"NewBuilding" bundle:nil];
new.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
new.modalPresentationStyle=UIModalPresentationFormSheet;
[self.navigationController presentModalViewController:new animated:YES];
new.view.superview.frame = CGRectMake(0, 0, 357 ,117);//it's important to do this after. Take a frame size exactly of your new viewController's size.
new.view.superview.center = self.view.center;
[new release];
}
This is how my NewBuilding viewController will appear on screen.
Edit 1:
In this written in presentModalViewController reference: "On iPhone and iPod touch devices, the view of modalViewController is always presented full screen." so for iPhone it may not serve your purpose.
if you want to display less details then you can use following option.
declare below code in .h file
UIWindow *alertWindow;
Write following code in .m file.
alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // you must release the window somewhere, when you do not need it anymore
alertWindow.windowLevel = UIWindowLevelAlert; // puts it above the status bar
alertWindow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
/*CUSTOM VIEW obj*/.center = CGPointMake(alertWindow.frame.size.width/2, alertWindow.frame.size.height/2);
[alertWindow addSubview:/*PUT YOUR CUSTOM VIEW HERE*/];
[alertWindow setHidden:NO];
this code is display you custom view like UIAlertView.
you put above code in UITableView Delegate method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
For hide window
[alertWindow setHidden:YES];
[alertWindow release]; alertWindow = nil;

iPhone:DatePicker dd/mm/yyyy

I am using a DatePicker with popups when a user click a textField. However, the Picker displays Month first, not date first and it isn't British way to write a date.
Is there any way to set the date format in DatePicker to British way such as dd/mm/yyyy?
I am using an Achtionsheet:
-(IBAction)acsheet:(id)sender
{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
//CGRect pickerFrame = CGRectMake(0, 45, 0, 0);
pickerView1 = [[UIDatePicker alloc] init];
pickerView1.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:pickerView1];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
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];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[pickerView1 addTarget:self
action:#selector(updateLabel:)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDatePicker:)] ;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
}
The UIDatePicker, by default, uses whatever [NSLocale currentLocale] dictates.
The locale property on UIDatePicker was deprecated in iOS 5, but I believe that you could do the following:
NSCalendar *cal = [NSCalendar currentCalendar];
cal.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
myDatePicker.calendar = cal;
This should work. If it doesn't, please file a bug and I'll fix it. :)
The UIDatePicker is made to adapt to the user's settings. Changing its format using the locale: method was possible but is now deprecated in iOS 5.0.
You can view your format in the settings of your iPhone :
Settings > General > International > Region Format
If you want a specific format, consider making your own picker but it is very discouraged for a date picker.
When writing the month in text the british probably would put the month first (or at least it would be acceptable).
But to answer your question I'm not aware of a way to change the display format of the date picker, but you could make you're own using a UIPickerView with a custom delegate and data source.

UIPopoverController without arrows?

I would like to know to make an UIPopoverController without arrows
In fact I would like to simulate something like this:
See that
There is no arrows
There is a title that is somehow inside of a expanded top border of the UIPopoverController and not inside of it like in the normal UIPopoverController.
I assume this is not really an UIPopoverController object but I would appreciate advices on how can I make the same effect (using CoreGraphics? -> specially the translucent degrade effect of the 3D outstanding border) and/or links to some sources if anyone has done this before.
Thanks in advance.
Ignacio
EDIT:
I am still looking for this stuff and realized that even in third party apps is being used
an example is: twitterrific for iPad as seen in this picture.
Anyone please? Putting the title inside the popovercontroller is just ugly.
The below method works fine for me (include iOS7)
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 20, 20)
inView:self.view
permittedArrowDirections:NULL
animated:YES];
Pass 0 to permittedArrowDirections attribute.
[popoverController presentPopoverFromRect:YOUR_RECT
inView:self.view
permittedArrowDirections:0
animated:YES];
While there is some question about whether Apple will approve apps that create a popover without an arrow, you might want to check out this post regarding arrows and this post regarding modal views.
To create a popover with a title you need to create a separate view like you would make a separate window and then load that view in the popover.
The top border is produced by placing a navigation controller between the popover and the presented view controller.
In other words, the popover presents a navigation controller and the navigation controller's root view controller is set to your view controller. This produces the title bar and allows you to set the title with [self setTitle:#"My Title"] and add navigation buttons.
You can add a title by using a UINavigationController, and adding UIViewControllers to the navigation controller. Set the 'title' attribute of the UIViewController to make the title appear.
Setting the arrow direction to NULL, as some have suggested, can result in unpredictable behavior, since the method uses this variable to figure out how to orient the popup relative to your bar button item or rectangle.
It is better to subclass UIPopoverBackgroundView, and set the various arrow return methods to return 0 for the arrows (iOS5 and up only). See this example for how to subclass this properly:
http://blog.teamtreehouse.com/customizing-the-design-of-uipopovercontroller
Simple implementation example (MyCustomPopoverBGView is the subclass of UIPopoverBackgroundView in this example):
UIViewController *vCtrlr = [[UIViewController alloc] initWithNibName:nil bundle:nil];
vCtrlr.title = #"My Title";
self.navCtrlr = [[UINavigationController alloc] initWithRootViewController:vCtrlr];
self.popCtrlr = [[UIPopoverController alloc] initWithContentViewController:_navCtrlr];
_popCtrlr.popoverBackgroundViewClass = [MyCustomPopoverBGView class];
[_popCtrlr presentPopoverFromRect:CGRectMake(0,
0,
320,
150)
inView:self permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Just copy & Paste the below code
UIViewController *popovercontroller=[[UIViewController alloc] init];
UIView *popoverView=[[UIView alloc] initWithFrame:CGRectMake(312,390, 400, 344)];
popoverView.backgroundColor=[UIColor whiteColor];
popovercontroller.contentSizeForViewInPopover=CGSizeMake(400, 300);
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 400, 0)];
[pickerView setTintColor:[UIColor blackColor]];
[pickerView addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
NSString *bs ; //= [NSString alloc];
// //NSDate *newDate = [NSData alloc];
bs = CurrentSelectedDate;
if (bs.length >= 1) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
// //[dateFormatter setDateStyle:NSDateFormatterLongStyle];
// [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"dd-MMM-yyyy"];
// NSDate *myDate = [dateFormatter dateFromString: txtText.text];
pickerView.date = [dateFormatter dateFromString: CurrentSelectedDate];
}
else
{
pickerView.date = [NSDate date];
}
[popoverView addSubview:pickerView];
// pickerView.date = [dateFormatter dateFromString:txtText.text];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
pickerToolbar.barStyle = UIBarStyleDefault;
pickerToolbar.barTintColor=[UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
[pickerToolbar sizeToFit];
self.navigationController.toolbar.barTintColor = [UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
doneBtn.tintColor=[UIColor whiteColor];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
cancelBtn.tintColor=[UIColor whiteColor];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
popovercontroller.view=popoverView;
pickerViewPopup = [[UIPopoverController alloc] initWithContentViewController:popovercontroller];
[pickerViewPopup presentPopoverFromRect:CGRectMake(312, 212, 400, 344) inView:self.view permittedArrowDirections:0 animated:YES];