ios datepicker crashes randomly - ios

I have a UIDatepicker and it crashes randomly in iOS7. At the simulator, the UIDatepicker works perfectly but on the real device always crash. The error is EXC_BAD_ADRESS but the Xcode doesn't give me more information.
Please help me!
My code is:
VistaRuedaViewController.h
#interface VistaRuedaViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,UITabBarControllerDelegate, UITabBarDelegate>
{
NSString *fecha;
Utilidades *util;
BOOL conexion;
UIDatePicker *datePicker;
}
#property (nonatomic, retain) IBOutlet UIButton *button;
#property (nonatomic, retain) IBOutlet UITableView *table;
#property (nonatomic, strong) IBOutlet UIDatePicker *datePicker;
#property (nonatomic, retain) IBOutlet UILabel *labelAviso;
#property (nonatomic, retain) IBOutlet UITabBar *tabbar;
-(IBAction)SelectSend:(id)sender;
#end
VistaRuedaViewController.m
- (void)viewDidLoad
{
...
datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
self.datePicker.minimumDate = [NSDate date];
datePicker.datePickerMode = UIDatePickerModeDate;
...
}
-(IBAction)SelectSend:(id)sender{
UIDatePicker *dp = (UIDatePicker *) sender;
//[table reloadData];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateFormat:#"dd/MM/yyyy"];
fecha =[formatter stringFromDate:dp.date];
NSLog(#"FECHA DATAPICKER: %#", dp.date);
NSString *idioma;
NSString *currentL = [Global sharedMySingleton].test;
if([currentL isEqualToString:#"ca-ES"]){
idioma = #"ca";
}
else
{
idioma = currentL;
}
button.enabled = NO;
NSString *PlaningURLString = [NSString stringWithFormat:#"%#%#/PlaningHorario", NSLocalizedString(#"protocolo", #""), NSLocalizedString(#"servidor", #"")];
util = [[Utilidades alloc]init];
conexion = [util testInternetConnection];
if (conexion) {
Planing_pasarela *cargaPlaning = [[Planing_pasarela alloc] init];
[cargaPlaning NSURLConnectionFunction:PlaningURLString:fecha:fecha:idioma];
int ret =[cargaPlaning comprobarFlag];
button.enabled = YES;
if (ret==1) {
[table reloadData];
NSLog(#"DATAPICKER: %#", fecha);
}else{
// Usuario y token incorrectos
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:AMLocalizedString(#"logoutAlert", #"") message:AMLocalizedString(#"logout", #"") delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
alert.tag = 1;
[alert show];
}
}
}
Thank you for advance.

#property (nonatomic, strong) IBOutlet UIDatePicker *datePicker;
Why it's strong when other oultest is retain. You are using ARC or what?
If you use ARC, than all oultets shold be strong, if not then retain.
And if you use outlet, you don't need allocate it: datePicker = [[UIDatePicker alloc]init];
Use everywhere seld.datePicker instead datePicker.
You don't need UIDatePicker *datePicker; inside #interface VistaRuedaViewController ...{ }
If App is non-ARC, then you need release formatter and util

Related

how to change FSCalandar date color for the specific date.?

Hi all i am working with FSCalandar in my project. Everything is working fine but i am not able change calendar date color for the date which will entered by user. Basically i want to add the functionality of present and absent in my project using FSCalander. i already called fscalanderappearance delegate method.Please look at my code.
This is .h file
#interface CalTableCellTableViewCell : UITableViewCell <FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance>
{
NSDate *myDate;
NSDate *EndDate;
NSMutableArray *array;
}
#property (weak , nonatomic) FSCalendar *calendar;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *calendarHeightConstraint;
#property (weak, nonatomic) IBOutlet UIView *calview;
#property (strong, nonatomic) NSDate *selectedDate;
#property (strong, nonatomic) NSDictionary *fillSelectionColors;
#property (strong, nonatomic) NSDictionary *fillDefaultColors;
#property (strong, nonatomic) NSDictionary *borderDefaultColors;
#property (strong, nonatomic) NSDictionary *borderSelectionColors0;
#property (strong, nonatomic) NSArray *datesWithEvent;
#property (strong, nonatomic) NSArray *datesWithMultipleEvents;
#end
This is .m file.
#import "CalTableCellTableViewCell.h"
#implementation CalTableCellTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:#"myDateKey"];
NSLog(#"mydate %#", myDate);
EndDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:#"EndDateKey"];
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, 250, 150)];
calendar.dataSource = self;
calendar.delegate = self;
[self.contentView addSubview:calendar];
self.calendar = calendar;
_calendar.scrollDirection = FSCalendarScrollDirectionVertical;
calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"d.M.yyyy";
NSString *string = [formatter stringFromDate:[NSDate date]];
NSLog(#"current date %#", string);
_calendar.appearance.selectionColor = [UIColor blueColor];
_calendar.appearance.todayColor = [UIColor greenColor];
_calendar.appearance.cellShape = FSCalendarCellStyleRectangle;
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
NSArray *testarray = [userDef objectForKey:#"name"];
array = [[NSMutableArray alloc]initWithArray:testarray];
NSLog(#"arraydata %#", array);
self.datesWithEvent = #[#"2016-09-03",
#"2015-10-06",
#"2015-10-12",
#"2015-10-25"];
self.datesWithMultipleEvents = #[#"2015-10-08",
#"2015-10-16",
#"2015-10-20",
#"2015-10-28"];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
_calendarHeightConstraint.constant = CGRectGetHeight(bounds);
[self.contentView layoutIfNeeded];
}
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
return myDate;
}
- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
return EndDate;
}
#pragma mark - <FSCalendarDelegateAppearance>
- ( UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date;
{
NSString *datestring = [calendar stringFromDate:myDate format:#"yyyy-MM-dd"];
if ([_datesWithEvent containsObject:datestring]) {
NSLog(#"test");
return calendar.appearance.borderDefaultColor = [UIColor redColor];
}
else if ([_datesWithMultipleEvents containsObject:datestring])
{
NSLog(#"testing");
}
return nil;
}
Please help if anyone go with this problem. thanks. Delegates method not called.
You can apply FSCalendar Data, Delegate & FSCalendarDelegateAppearance to the TableViewController or to the ViewController that contains TableView instead of implementing it in UITableViewCell. Then override the method titleDefaultColorForDate as shown below. It works for me.
-(UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date{
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateFormat = #"yyyy/MM/dd";
NSString *CurrentDate = [self.formatter stringFromDate:date];
NSString *Today = [self.formatter stringFromDate:[_Calendar today]];
//_bookedDates are array of specific dates
if([_bookedDates containsObject:CurrentDate])
//if booking dates contains currentdate change the color of the title
return [UIColor whiteColor];
else
return appearance.titlePlaceholderColor;
}

Error with segue: 'NSInvalidArgumentException', reason: 'Receiver has no segue with identifier

I'm getting this error with my app: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<ViewController: 0x17e60c10>) has no segue with identifier 'dateTimeString''. Code is this:
ViewController.m
-(IBAction) alarmSetButtonTapped:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
[dateFormatter setDateFormat:#"dd:MM:YYYY:ss:mm:hh"];
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSLog( #"Alarm Set : %#", dateTimeString );
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd:MM:YYYY:ss:mm:hh"];
NSDate *dateTimeSeconds = [[NSDate alloc] init];
dateTimeSeconds = [dateFormatter1 dateFromString:dateTimeString];
NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:dateTimePicker.date];
NSLog(#"seconds %.f", seconds);
NSString *path = [NSString stringWithFormat:#"%#/Alarm-Clock.m4a", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
_alarmPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_alarmPlayer playAtTime: _alarmPlayer.deviceCurrentTime + seconds];
NSLog( #"Alarm Set button tapped : %#", dateTimeString );
[self scheduleLocalNotificationWithDate: dateTimePicker.date];
[self performSegueWithIdentifier:#"dateTimeStringSegue" sender: nil];
[self presentMessage:(#"Alarm succesfully set for %#",dateTimeString)];
}
-(IBAction)alarmCancelButtonTapped:(id)sender {
NSLog( #"Alarm Cancel button tapped");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self presentMessage:#"Alarm Canceled Lazy Pants!"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ViewController3 *tvc = segue.destinationViewController;
NSLog(#"prepareForSegue: %#", segue.identifier);
if ([[segue identifier] isEqualToString:#"dateTimeStringSegue"])
{
tvc.dateTimeString = (#"%#",_dateTimeString);
NSLog(#"Segue");
}
}
ViewController.h
#interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
IBOutlet UIDatePicker *dateTimePicker;
}
#property NSInteger numberOfLoops;
#property(readonly) NSTimeInterval deviceCurrentTime;
#property (strong, nonatomic) AVAudioPlayer *alarmPlayer;
#property (strong, nonatomic) NSString *dateTimeString;
- (NSTimeInterval)timeIntervalSinceDate:dateTimePicker;
-(void) presentMessage: (NSString *) message;
-(void) scheduleLocalNotificationWithDate: (NSDate *) fireDate;
-(IBAction) alarmSetButtonTapped:(id)sender;
-(IBAction) alarmCancelButtonTapped:(id)sender;
- (BOOL)playAtTime:(NSTimeInterval)time;
#end
SegueDestination.h
#import "ViewController.h"
#interface ViewController3 : UIViewController
#property(nonatomic, readonly) NSUInteger tapCount;
#property NSInteger numberOfLoops;
#property(readonly) NSTimeInterval deviceCurrentTime;
#property (strong, nonatomic) AVAudioPlayer *alarmPlayer;
#property (strong, nonatomic) NSString *dateTimeString;
#property (strong, nonatomic) IBOutlet UILabel *dateTimePicker;
- (NSTimeInterval)timeIntervalSinceDate:dateTimePicker;
- (BOOL)playAtTime:(NSTimeInterval)time;
- (IBAction)iconsBtn:(id)sender;
#end
SegueDestination.m
#import "ViewController.h"
#interface ViewController3 ()
{
AVAudioPlayer *_alarmPlayer;
}
#end
#implementation ViewController3
- (void)viewDidLoad {
[super viewDidLoad]
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"wwv5.jpg"]];
}
- (void) buttonTouchDownRepeat:(id)sender event:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if(touch.tapCount == 3) {
NSLog(#"Twice");
}
else
NSLog(#"UGHHHHH");
}
- (IBAction)iconsBtn:(id)sender {
}
I'm trying to access the dateTimeString in my 2nd ViewController but whenever I click the button that is supposed to send it my app terminates with that error. This is the first time I've attempted to use segue and I'm not going so great with it. Once this error is sorted will I be able to call dateTimeStringin the 2nd ViewControllerand get the value set in the 1st ViewController?
Thanks!
Edit: I have already done the identifier in storyboard.
In your storyboard you need to set Identifier for your segue:

iOS adding variable to label's text

I've been working on an iOS app and I've run into some complications in one of the screens.
This screen, titled "about", has just 2 labels: they're supposed to display the current username and time.
The labels' text loads well but I can't get them to show me the variables with the username and the current time.
Here is my code:
About.h
#interface About : UIViewController
#property (weak) IBOutlet UILabel * username;
#property (weak) IBOutlet UILabel * date;
#property (strong, nonatomic) NSString * usuari;
#property (strong, nonatomic) NSString * data;
#property (strong, nonatomic) NSString * name;
#property (strong, nonatomic) NSString * currentTime;
About.m
#import "About.h"
#interface About()
#end
#implementation About
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel * username = [[UILabel alloc] initWithFrame:CGRectMake(162, 106, 150, 72)];
[self.view addSubview: username];
username.text = #"Usuari: ";
username.numberOfLines = 4;
username.textColor = [UIColor blackColor];
username.text = [NSString stringWithFormat:#"Usuari: ", _usuari];
UILabel * date = [[UILabel alloc] initWithFrame:CGRectMake(160, 261, 1488, 44)];
[self.view addSubview: date];
date.text = #"Hora: ";
date.numberOfLines = 4;
date.textColor = [UIColor blackColor];
date.text = [NSString stringWithFormat:#"Hora: ", _currentTime];
}
-(void) gettingUser
{
[[UIDevice currentDevice] name];
self.username.text = self.name;
}
-(void) gettingTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]];
self.date.text = self.currentTime;
//self.date.text = [NSDate date];
//NSTimer schedule interval
//NSDateFormatter alloc
}
#end
Can you help me figure out what do I need to do to make it work?
The reason the variable are not showing is due to no format specifiers. I'm also surprised you didn't receive compiler warnings or errors with the above code.
This line of code:
username.text = [NSString stringWithFormat:#"Usuari: ", _usuari];
Should be written, like this - not so?
username.text = [NSString stringWithFormat:#"Usuari:%# ", _usuari];
As _usuari is the variable and the %# is the format specifier, of where you want to place that variable.
After this: ursername.text should print out like so: Usuari: variable value

Parsing a Date string with AFNetworking (dateFormatter)

I'm changing my app so it could parse my JSON through AFNetworking and I'm getting an error when I try to change my date string with dateFormatter. I have a NSObject called "UpcomingRelease", a CollectionViewController called "UpcomingReleasesViewController" and a destinationViewController called "ReleaseViewController"
This my old code:
* UpcomingRelease.h
#interface UpcomingRelease : NSObject
#property (nonatomic, strong) NSString *release_date;
- (NSString *) formattedDate;
#end
* UpcomingRelease.m
- (NSString *) formattedDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *newDate = [dateFormatter dateFromString:self.release_date];
[dateFormatter setDateFormat:#"MMMM dd"];
return [dateFormatter stringFromDate:newDate];
}
* ReleaseViewController.h (destinationViewController)
#import "UpcomingRelease.h"
#property (strong, nonatomic) NSDictionary *singleRelease;
#property (weak, nonatomic) IBOutlet UILabel *release_date;
* ReleaseViewController.m
#synthesize singleRelease = _singleRelease;
#synthesize release_date = _release_date;
- (void)viewDidLoad
{
[super viewDidLoad];
if([_singleRelease objectForKey:#"release_date"] != NULL)
{
self.release_date.text = [NSString stringWithFormat:#"%#", _singleRelease.formattedDate];
}
else
{
self.release_date.text = [NSString stringWithFormat:#"Releasing Soon"];
}
}
I get an error saying "Property 'formattedDate' not found on object of type 'NSDictionary').
The error already says that NSDictionary doesn't have a property called formattedDate. And right now you are calling _singleRelease.formattedDate and _singleRelease is a NSDictionary.
Change your code to the following:
if([_singleRelease objectForKey:#"release_date"] != NULL)
{
NSString *rd = [_singleRelease objectForKey:#"release_date"]; // I assume that this is a string
UpcomingRelease *upcoming = [[UpcomingRelease alloc] init];
upcoming.release_date = rd;
self.release_date.text = [NSString stringWithFormat:#"%#", upcoming.formattedDate];
}

Parsing a FormattedDate on a destinationView

I'm parsing some information to my app using JSON. I'm able to show the formattedDate on my Main View Controller but when I want to do it on my destinationView page (UpcomingReleaseViewController), I get a "null" string. This is what I have so far:
UpcomingRelease.h
#property (nonatomic, strong) NSString *release_date;
- (NSString *) formattedDate;
UpcomingRelease.m
- (NSString *) formattedDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *tempDate = [dateFormatter dateFromString:self.release_date];
[dateFormatter setDateFormat:#"EE MMM,dd"];
return [dateFormatter stringFromDate:tempDate];
}
UpcomingReleaseViewController.h
#property (strong, nonatomic) UpcomingRelease *singleRelease;
#property (weak, nonatomic) IBOutlet UILabel *release_date;
UpcomingReleaseViewController.m
#synthesize singleRelease = _singleRelease;
#synthesize release_date = _release_date;
- (void)viewDidLoad
{
[super viewDidLoad];
self.release_date.text = [NSString stringWithFormat:#"%#", _singleRelease.formattedDate];
}
JSON
release_date: "2013-09-28T00:00:00.000Z"
Thanks.
In UpcomingRelease.m you need to parse the date's 'T' and 'Z' as literal characters and account for the fractional seconds:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];

Resources