I need some help related to iOS application
On clicking the button I added a DatePicker programmatically and now I am not able to remove the view this is my code
I also tried to put the date in label dateLabel but it shows some run time error because it is not a string format I tried some other method also like NSDateFormatter but I am not getting the proper result
-(IBAction)date:(id)sender{
UIDatePicker *datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(420, 320, 300, 500)];
[datePicker addTarget:self action:#selector(Pick:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
NSData *date=[datePicker date];
NSLog(#"%#",date);
dateLabel.text=date;
}
-(void)Pick:(id)sender{
[datePicker removeFromSuperview];
}
Do like this
-(IBAction)date:(id)sender
{
datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(420, 320, 300, 500)];
[datePicker addTarget:self action:#selector(Pick:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
-(void)Pick:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterShortStyle;
label.text=[NSString stringWithFormat:#"%#",
[df stringFromDate:datePicker.date]];
[df release];
[datePicker removeFromSuperview];
datePicker=nil;
}
You are declaring the UIDatePicker *datePicker as a local variable.
It can be only accessible in the declared function (-(IBAction)date:(id)sender).
You need to declare it globally for using it in other functions.
Declare that variable in your .m file under
#interface className()
#property(nonatomic, strong) UIDatePicker *datePicker;
#end
Try this
Declare your datepicker in .h
UIDatePicker *datePicker;
and then
-(IBAction)date:(id)sender
{
datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(420, 320, 300, 500)];
[datePicker addTarget:self action:#selector(Pick:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
-(void)Pick:(id)sender
{
NSDate *date=[datePicker date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"YYYY-MM-DD"];
NSString *dateSTr = [dateformatter stringFromDate:date];
NSLog(#"%#",dateSTr);
dateLabel.text=date;
[datePicker removeFromSuperview];
}
Related
I am facing a problem in date picker. when am i clicking on date picker done button. then my code is crashing.
-(IBAction)btn:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:[NSDate date]]];
[df release];
[self.view addSubview:datelabel];
[datelabel release];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:#selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:datePicker.date]];
}
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,4,datePicker.frame.size.width,40);
[datePicker addSubview:toolbar];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(DoneSelected)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace,doneBtn,nil]];
[flexibleSpace release];
}
-(void)DoneSelected
{
your implementation code
}
I am developing an universal app for iOS so that I have to set label to center in UIToolBar. Please help me to do this.
The code I have implemented
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
label.backgroundColor = [UIColor clearColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
label.text = dateString;
FirstViewController.h
#interface FirstViewController:UIVieWController
{
UILable *justLabel;
}
#end
In FirstViewController.m try The fallowing code .Here I am taking timer for displaying time on the UIToolbar
#implementation FirstViewController
-(void)viewDidLoad
{
UIToolbar *tool=[[UIToolbar alloc] initWithFrame:CGRectMake(0,372, self.view.frame.size.width, 44)];
justLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 4, 250, 40)];
justLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:tool];
[tool addSubview:justLabel];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timer) userInfo:nil repeats:YES];
}
-(void)timer
{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
NSDate *now = [NSDate date];
NSString *dateString = [format stringFromDate:now];
justLabel.text=dateString;
}
#end
You have to add flexible space bar button items before and after the label, this will center the label in your toolbar.
The flexible space is one of the system items when creating UIBarButtonItems. When you set the items of your toolbar, it should be an array containing a flexible space, your label, and another flexible space.
Set frame of label according to the view width....
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-250)/2, 0, 250, 40)];
here in (self.view.frame.size.width-250)/2 250 is your required label width.....
By setting page with i am
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width)-55, 20)];
In my application i am using pickerview
My problem is that when in iOS 4.3 Picker view comes MM-dd-yy format,
in iOS 5.1 Picker view comes dd-MM-yy format,
I want MM-dd-yy format in iOS 5.1
My code is below,
I created picker view programmatically with this code,
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.date = [NSDate date];
NSDate *today = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM-dd-yy"];
NSString *dateString = [df stringFromDate:today];
dateString = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
Anybody suggest me how can i resolve this issue on iOS 5.1?
My all code works is fine,
Problem is with Settings,
My region format is set to india and change it to United state,
All works fine.....
Check this
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.date = [NSDate date];
[datePicker addTarget:self action:#selector(LabelChange:)forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
For this you can make your custom picker.
Or you want to format the date then use this code..
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-yy"];
NSString *date = [dateFormat stringFromDate:datePicker.date];
NSLog(#"date is >>> , %#",date);
Change region to United states i.e locale settings
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"US"];
[datePicker setLocale:locale];
In iOS 5.1 display UIDatePicker as it is in 4.3
Your and mine both of code is same but in my application UIDatePicker display
with MM-dd-yyyy
Use it ...may be helpful for you.
self.DatePicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
self.DatePicker.datePickerMode = UIDatePickerModeDate;
[self.DatePicker addTarget:self
action:#selector(SetDatePickerTime:)
forControlEvents:UIControlEventValueChanged];
[self.asheet addSubview:self.DatePicker];
How would you do that?
I have a form with a UITextField. When I click in it I would like to display a UIDatePicker instead of the default Keyboard that pops-up by default? Note that there are also other elements on my form.
I think this is a more official way to do this:
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
textField.inputView = datePicker;
in .h
UIDatePicker *datePicker;
NSDate *date;
UIToolbar *keyboardToolbar;
in ViewDidLoad:
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:#"Accept" style:UIBarButtonItemStyleDone target:self action:#selector(closeKeyboard:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, accept, nil]];
}
self.txtDate.inputAccessoryView = keyboardToolbar;
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.txtDate.inputView = datePicker;
- (void)datePickerValueChanged:(id)sender{
date = datePicker.date;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/YYYY"];
[self.txtDate setText:[df stringFromDate:date]];
}
in storyboard add to txtDate -> EditingDidEnd
- (IBAction)establishHour:(id)sender{
hour = hourPicker.date;
NSDateFormatter *hf = [[NSDateFormatter alloc] init];
[hf setDateFormat:#"HH:MM"];
[self.txtHour setText:[hf stringFromDate:hour]];
}
Sets the keyboard as DatePicker and created a toolbar with a button to accept the selection.
I've converted Johan Kool's answer into swift if anyone would like it. I placed the code in viewDidLoad.
let datePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
textField.inputView = datePicker
(IBAction)estableceHora:(id)sender{
Hora = horaPicker.date;
NSDateFormatter *hf = [[NSDateFormatter alloc] init];
[hf setDateFormat:#"HH:MM"];
[self.txtHora setText:[hf stringFromDate:Hora]];
}
Added in .h files :
UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
Added in .m files :
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
pTxtDate.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:[NSDate date]]];
NSLog(#"pTxtDate.text %#",pTxtDate.text);
[df release];
[self.view addSubview:pTxtDate];
[pTxtDate release];
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
DatePicker.datePickerMode = UIDatePickerModeDate;
DatePicker.hidden = NO;
DatePicker.date = [NSDate date];
[DatePicker addTarget:self
action:#selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:DatePicker];
[DatePicker release];
}
- (void)LabelChange:(id)sender {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
pTxtDate.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:DatePicker.date]];
}
Please help me to get the value of UIDatePicker to UILabel on button click for an iPhone application.
Create an outlet and then just ask its value
-(IBAction)OnButtonClick:(id)sender
{
NSDate *date = datePicker.date;
label.text = [date description];
}
DatePicker=[[UIDatePicker alloc]init];
[DatePicker setDatePickerMode:UIDatePickerModeDate];
//[DatePicker setMaximumDate:[NSDate date]];
[DatePicker setMinimumDate:[NSDate date]];
[DatePicker addTarget:self action:#selector(updateDate:)
forControlEvents:UIControlEventValueChanged];
-(void) doneButton
{
[self updateDate:DatePicker]; //calling this fuction on inside the button
}
-(void)updateDate:(UIDatePicker *)sender
{
NSDate *date = self.DatePicker.date;
_dateFormat = [[NSDateFormatter alloc]init];
[_dateFormat setDateFormat:#"MM/dd/yyyy"];
DateTextField.text = [_dateFormat stringFromDate:date];
}
Swift 2.0
#IBAction func buttonPressed(sender: AnyObject) {
let date: NSDate = timePicker.date
label.text = date.description
}