I am working on an app and I have this code:
NSDateFormatter *clockFormat = [[NSDateFormatter alloc]init];
[clockFormat setDateFormat:#"hh:mm a"];
self.clockLabel.text = [clockFormat stringFromDate:[NSDate date]];
[self performSelector:#selector(updateClockLabel) withObject:self afterDelay:1.0];
I created a view with subviews to create a LCD type clock display and I want to hide the correct subviews corresponding to the clockLabel. I created arrays of each view (digit), now how do I tell the views that if the first h in hh:mm is 1, hide these subviews to make an LCD "1" in the view.
Simply, how do I access each digit of hh:mm?
You can use NSRange to get substrings. Use NSRangeMake(location,length). To get an NSString of the first character of self.clockLabel.text. You can say say
NSString *firstHourDigit = [self.clockLabel.text substringWithRange:NSRangeMake(0,1)];
Related
I have a view controller that implements UITextViewDelegate, UITextFieldDelegate, and NSFetchedResultsControllerDelegate. It is a UIViewController, not a table VC. Nor is there a table in it.
When I make a change to the Core Data store in another view controller, most text fields and text views in the first VC update However, I have another text field that shows the date and is derived from `NSDateFormatter, based on a date attribute in Core Data. It is hidden when nil and when the value is no longer nil, the code to unhide it is not working.
There isn't any code in the VC that says the text views and text fields that DO update specifically are delegates of anything. They just update naturally.
Perhaps the delegate was set in storyboard, but I don't remember and don't know a way to check what delegates are set in storyboard.
Is there a special date delegate? Or how would I get this text field derived from NSDateFormatter to update?
EDIT:
This is where the date is displayed in a method called updateInterface called within viewWillAppear
//Here is an example of a label that does update when data changes.
self.nameLabel.text = self.item.name; //this sets name...It updates when name is changed.
//Here is the data label that is not changing from hidden to not hidden.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE MMMM d, YYYY 'at' h:mma"];
NSString *date = [dateFormatter stringFromDate:self.item.date];
if (self.item.date!=nil) {
self.dateLabel.text =date;
else {
self.dateLabel.hidden = YES;
}
I am Trying to assign the 24 hour formate to UIDATEPICKERVIEW but i have not exactly idea to implement it.
in my view.h file
#property(nonatomic,retain) IBOutlet UIDatePicker *picker_alarm;
in my view.m file
picker_alarm.date=[NSDate date];
in my .xib file
i have taken uidatepicker and its Mode Property to Time in viewcontroller.
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"NL"];
[picker_alarm setLocale:locale];
You can also use the storyboard to change time format to 24 hour.
Follow these steps:
go to storyboard
select you datePicker
select attributes inspector
Change "Locale" property to English(Europe)
and in your code use following snippet:
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"HH:mm"];
somelabel.text = [outputFormatter stringFromDate:_pickerDate.date];
You cannot force the date picker to show 24 hours time. That depends on the locale property
Doc says:
UIDatePickerModeTime, // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
[datePicker setLocale:[NSLocale systemLocale]];
For testing this change the system time to 24hour mode. And check the picker in simulator
Create DatePicker with setting it custom Locale
let datePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.time
datePicker.locale = Locale.init(identifier: "NL") //or: Locale.init(identifier:en_GB") // using Great Britain
Grab the time from above DatePicker,
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
lblTime.text = dateFormatter.string(from: sender.date)
Let me begin to tell you that I am new to Objective C. I have just finished Big Nerd Ranch's book and i want to create a real simple and basic app to learn more.
My idea was to create an app that will calculate the weeks between 2 dates. I have created a class for that and tested it. That works.
As you can see below, I have created to views (programmatically), One with the dates and the other will become visible when you click on start or end date.
If you select a date and click on the button 'calculate weeks', you will go back to the first view.
No my big question is, how do I get this selected value back to my main screen? I have tried several possibilities and search the web for information, but I couldn't get is to work.
I know this should be real easy, but for me at this moment it isn't. :-)
I have created a NSMutableArray that contains the values "Start date" and "End date". My idea was to add the value of the UILabel from the SelectDateView to this array.
I have created a property In the inputview #property (readwrite, retain) NSMutableArray *datesArray; for that.
in the selectDateViewController i have created another property #property (nonatomic, assign) BITInputViewController *ivc; so I (in my opinion) can add a value to datesArray.
When I select a date this method is called, it works for the UILabel on SelectDateView, but doesn't do anything with the datesArray.
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
dateLabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:datePicker.date]];
[ivc.datesArray addObject:dateLabel.text];
if (ivc.datesArray) {
for (NSString *d in ivc.datesArray) {
NSLog(#"This is in datesArray %#",d);
}
}else NSLog(#"!ivc.datesArray");
}
When I test the app and select a date, I always see "!ivc.datesArray" in the log file.
I also do this check of ivc.datesArray in -(void)viewWillAppear:(BOOL)animated
and here I see the current date, as I set this date in - (void)viewDidLoad
with this dateLabel.text = [NSString stringWithFormat:#"%#",[df stringFromDate:[NSDate date]]];
(When I print out the array in the inputview, it does show start date and end date, but not the selected date. )
Hopefully someone can give me a few pointers on this.
declare a NSString* date above the #interface in your view1.m file and the create a method in tht first class it should be something like
-(void)passDate:(NSString *)dateString
{
date = [NSString stringWithFormat:#"%#",dateString];
}
and in your second class create object of first class like
View1 * vc = [[View1 alloc]init];
[vc passDate:df];
Hope it works. You have to declare NSString before the #interface and no property and synthesize.
I had a similar question yesterday and I got part of my answer from there. But the thread got closed so this is kinda my follow-up question thread :)
original Q: How can I have a toolbar button/item change its date with [NSDate date]?
original A:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
yourToolBarItem = [NSString stringWithFormat:#"%#", [formatter stringFromDate:[NSDat date]]];
Followup Q: I have the toolbar made directly from navigationcontroller in storyboard . How can I reach its items programaticly and change item/object at index 0 to the current date.
I can't get my head around this. I have tried loads of types with [self navigationController.ToolbarItems] replacObjectAtIndexbut a big no for me so far.
Very helpful for any directive and help.
The easiest thing is to create a property for your button: #property IBOutlet UIBarButtonItem *myBarButton;
Then initialize it in viewWillAppear: myBarButton = [[UIBarButtonItem alloc] initWithTitle:(your date string) style: UIBarButtonItemStylePlain target:self action:(some action)];
// Of course you'll need to add it to the navigation bar here too...
Then when it's time to update the date on the button:
[myBarButton setTitle:(your date string)];
Hope that helps...
I noticed that Tapku calendar grid resized between 5x5 and 6x6 depending on the month's dates distribution. For example it displays 5x5 grid for March 2012 and displays 6x6 grid for September 2012! For me it makes GUI alignments go wrong so I want grid to be always 6x6. I have been looking into TKCalenderMonthView's rangeOfDatesInMonthGrid method but not getting how it creates the grid! Could anyone please tell me how to fix the grid 6x6 always.
Thanks.
Solution to 2nd Point.If you want to display a mark on selected and remove mark from deselected dates then use follwoing code.(It wont display blue mark)
- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d {
NSLog(#"calendarMonthView didSelectDate");
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *strqw=[df stringFromDate:d];
NSLog(#"%#",strqw);
NSString *stss=[strqw stringByAppendingString:#" 00:00:00 +0000"];
NSLog(#"%#",stss);
int k=0;
for (int i=0; i<[array1 count]; i++) {
if([[array1 objectAtIndex:i]isEqualToString:stss]){
[array1 removeObjectAtIndex:i];
k=1;
}
}
if(k==0)
[array1 addObject:stss];
[calendar reload];
}
And add this line
NSArray *data=[[NSArray alloc] initWithArray:array1];
in the below method
- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate
Here's a little hack I use:
In the viewDidAppear I have
[calendar selectDate:[NSDate dateWithTimeIntervalSinceNow:-31*24*60*60]];
[calendar selectDate:[NSDate dateWithTimeIntervalSinceNow:0]];
And in the
- (void) calendarMonthView:(TKCalendarMonthView*)monthView monthDidChange:(NSDate*)month animated:(BOOL)animated{
I just check the height of my calendar and do whatever I need to do. This is done because if you check the height of the frame immediately after instantiation of the calendar, it will return the normal 5*7 height instead of the 6*7.
Hope that helps!