iOS adding variable to label's text - ios

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

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;
}

ios datepicker crashes randomly

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

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'"];

How to show image from sqlite database into UIImageView

Im using an existing proyect downloaded from here
http://www.raywenderlich.com/913/sqlite-101-for-iphone-developers-making-our-app
I like to add a new column called "image1" as BLOB in the database banklist.sqlite3 because i need to show images in FailedBanksDetailViewController.xib.
I did this:
FailedBanksDetail.h
#import <Foundation/Foundation.h>
#interface FailedBankDetails : NSObject {
int _uniqueId;
NSString *_name;
NSString *_city;
NSString *_state;
int _zip;
NSDate *_closeDate;
NSDate *_updatedDate;
NSString *_image1;
}
#property (nonatomic, assign) int uniqueId;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *city;
#property (nonatomic, copy) NSString *state;
#property (nonatomic, assign) int zip;
#property (nonatomic, retain) NSDate *closeDate;
#property (nonatomic, retain) NSDate *updatedDate;
#property (nonatomic, copy) NSString *image1;
- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name city:(NSString *)city
state:(NSString *)state zip:(int)zip closeDate:(NSDate *)closeDate
updatedDate:(NSDate *)updatedDate image1:(NSString *)image1;
#end
FailedBanksDetail.m
#import "FailedBankDetails.h"
#implementation FailedBankDetails
#synthesize uniqueId = _uniqueId;
#synthesize name = _name;
#synthesize city = _city;
#synthesize state = _state;
#synthesize zip = _zip;
#synthesize closeDate = _closeDate;
#synthesize updatedDate = _updatedDate;
#synthesize image1 = _image1;
- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name
city:(NSString *)city state:(NSString *)state zip:(int)zip closeDate:(NSDate *)closeDate
updatedDate:(NSDate *)updatedDate image1:(NSString *)image1 {
if ((self = [super init])) {
self.uniqueId = uniqueId;
self.name = name;
self.city = city;
self.state = state;
self.zip = zip;
self.closeDate = closeDate;
self.updatedDate = updatedDate;
self.image1 = image1;
}
return self;
}
- (void) dealloc
{
self.name = nil;
self.city = nil;
self.state = nil;
self.closeDate = nil;
self.updatedDate = nil;
self.image1 = nil;
[super dealloc];
}
#end
FailedBankDetailViewController.h
#import <UIKit/UIKit.h>
#interface FailedBanksDetailViewController : UIViewController {
UILabel *_nameLabel;
UILabel *_cityLabel;
UILabel *_stateLabel;
UILabel *_zipLabel;
UILabel *_closedLabel;
UILabel *_updatedLabel;
int _uniqueId;
UIImageView *_image1View;
}
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UILabel *cityLabel;
#property (nonatomic, retain) IBOutlet UILabel *stateLabel;
#property (nonatomic, retain) IBOutlet UILabel *zipLabel;
#property (nonatomic, retain) IBOutlet UILabel *closedLabel;
#property (nonatomic, retain) IBOutlet UILabel *updatedLabel;
#property (nonatomic, assign) int uniqueId;
#property (nonatomic, retain) IBOutlet UIImageView *image1View;
#end
FailedBankDetailViewController.m
// In the #import section
#import "FailedBankDatabase.h"
#import "FailedBankDetails.h"
// In the #implementation section
#synthesize nameLabel = _nameLabel;
#synthesize cityLabel = _cityLabel;
#synthesize stateLabel = _stateLabel;
#synthesize zipLabel = _zipLabel;
#synthesize closedLabel = _closedLabel;
#synthesize updatedLabel = _updatedLabel;
#synthesize uniqueId = _uniqueId;
#synthesize image1View = _image1View;
// In the dealloc section AND the viewDidUnload section
self.nameLabel = nil;
self.cityLabel = nil;
self.stateLabel = nil;
self.zipLabel = nil;
self.closedLabel = nil;
self.updatedLabel = nil;
self.image1View = nil;
//In the viewWillAppear method
- (void)viewWillAppear:(BOOL)animated {
FailedBankDetails *details = [[FailedBankDatabase database]
failedBankDetails:_uniqueId];
if (details != nil) {
[_nameLabel setText:details.name];
[_cityLabel setText:details.city];
[_stateLabel setText:details.state];
[_zipLabel setText:[NSString stringWithFormat:#"%d", details.name]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM dd, yyyy"];
[_closedLabel setText:[formatter stringFromDate:details.closeDate]];
[_updatedLabel setText:[formatter stringFromDate:details.updatedDate]];
[_image1View "I DONT KNOW WHAT TO DO HERE"
}
}
FailedBankDatabase.m
// In the #import section
#import "FailedBankDetails.h"
// Anywhere inside the #implementation
- (FailedBankDetails *)failedBankDetails:(int)uniqueId {
FailedBankDetails *retval = nil;
NSString *query = [NSString stringWithFormat:#"SELECT id, name, city, state,
zip, close_date, updated_date, image1 FROM failed_banks WHERE id=%d", uniqueId];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int uniqueId = sqlite3_column_int(statement, 0);
char *nameChars = (char *) sqlite3_column_text(statement, 1);
char *cityChars = (char *) sqlite3_column_text(statement, 2);
char *stateChars = (char *) sqlite3_column_text(statement, 3);
int zip = sqlite3_column_int(statement, 4);
char *closeDateChars = (char *) sqlite3_column_text(statement, 5);
char *updatedDateChars = (char *) sqlite3_column_text(statement, 6);
"I DONT KNOW WHAT TO DO HERE"
NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
NSString *city = [[NSString alloc] initWithUTF8String:cityChars];
NSString *state = [[NSString alloc] initWithUTF8String:stateChars];
NSString *closeDateString =
[[NSString alloc] initWithUTF8String:closeDateChars];
NSString *updatedDateString =
[[NSString alloc] initWithUTF8String:updatedDateChars];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *closeDate = [formatter dateFromString:closeDateString];
NSDate *updateDate = [formatter dateFromString:updatedDateString];
"I DONT KNOW WHAT TO DO HERE"
retval = [[[FailedBankDetails alloc] initWithUniqueId:uniqueId name:name
city:city state:state zip:zip closeDate:closeDate
updatedDate:updateDate] autorelease];
[name release];
[city release];
[state release];
[closeDateString release];
[updatedDateString release];
[formatter release];
break;
}
sqlite3_finalize(statement);
}
return retval;
}
I have no problem with FailedBanksDetailViewController.xib, i put a UIImageView element from the library and i link it to image1View declared in FailedBanksDetailViewController.h.
But i dont know what to do setting FailedBankDatabase.m and FailedBankDetailViewController.m.
I need to show the image into the UIImageView from the database in FailedBankDetailViewController.
I think you want a NSData object for your image (e.g. via UIImagePNGRepresentation) and then use sqlite3_column_blob instead of sqlite3_column_text.

Resources