Xcode label text from date picked - ios

I have a calendar view that displays the days of the month, and I would like to put the output into my label but am unable to. I can NSLog it but not transfer it into a label. Here is my code:
- (void)calendarView:(DSLCalendarView *)calendarView didSelectRange:(DSLCalendarRange *)range {
if (range != nil) {
NSLog( #"%ld/%ld", (long)range.startDay.month, (long)range.endDay.day);
NSString *datechosen = ( #"%ld/%ld", range.startDay.month,range.endDay.day);
DatePicked.text = datechosen;
}
Any help would be appreciated.

try something like this
if(DatePicked)
{
DatePicked.text = datechosen;
}
else
{
NSLog(#"hey look, DatePicked is not connected to an IBOutlet");
}

DatePicked.text = [NSString stringWithFormat:"%ld/%ld",(long)range.startDay.mont, (long)range.endDay.day];
Pleaes read Formatting String Objects.

Related

custom NSExpression functions like sin(x)

I know how to add custom functions to NSNumber for NSExpression to work with it. But for use it i need to declarate a string like "FUNCTION(1, 'sin')". Is is any way to declarate it just like "sin(1)"?
No, you cannot extend the syntax understood by NSExpression(format:).
For advanced expression parsing and evaluating, use 3rd party solutions
such as DDMathParser.
The selected answer is, in my opinion, ridiculous. You can, of course, simply reformat your string to your desired custom function, no need to become dependent on an entire library.
In your case, something like the following would work just fine.
NSString *equation = #"2+sin(54.23+(2+sin(sin(3+5))))-4+(5-3)+cos(4)";//your equation here
NSArray *functionNames = #[#"sin", #"cos", #"tan"];//your supported functions here
for (NSString *functionName in functionNames) {
NSString *functionPrefix = [NSString stringWithFormat:#"%#(", functionName];
while ([equation containsString:functionPrefix]) {
int parensLevel = 1;
int functionParameterIndex = ((int)[equation rangeOfString:functionPrefix].location)+((int)functionPrefix.length);
int characterIndex = functionParameterIndex;
while (characterIndex < equation.length) {
NSString *character = [equation substringWithRange:NSMakeRange(characterIndex, 1)];
if ([character isEqualToString:#"("]) {
parensLevel++;
} else if ([character isEqualToString:#")"]) {
parensLevel--;
}
if (parensLevel == 0) {
break;
}
characterIndex++;
}
if (parensLevel != 0) {
break;//parens weren't balanced, error handle as needed
}
NSString *functionParameter = [equation substringWithRange:NSMakeRange(functionParameterIndex, characterIndex-functionParameterIndex)];
NSString *function = [NSString stringWithFormat:#"%#(%#)", functionName, functionParameter];
equation = [equation stringByReplacingOccurrencesOfString:function withString:[NSString stringWithFormat:#"FUNCTION(%#,'%#')", functionParameter, functionName]];
}
}
//po string = "2+FUNCTION(54.23+(2+FUNCTION(FUNCTION(3+5,'sin'),'sin')),'sin')-4+(5-3)+FUNCTION(4,'cos')"
I wrote this in Objective-C but it works converted to swift as well.

Expected method to read array element not found on object of type NSDictionary*

I know there's a lot of questions like this around, but I think my situation's a tad different.
int i = 0;
while (_data[#"VerticalState%i", i] != nil) {
// do things
i++;
}
For example, one 'level' that has 3 VerticalState properties will be implemented as such: VerticalState0, VerticalState1, VerticalState2.
I want to read in those values using that while loop condition above, and it should stop when i = 3. How can I make the idea of that code above work (with some other configuration obviously). FYI, _data is an NSDictionary* instance variable, already loaded with the plist information.
You appear to want to create a dictionary key from a string format. You need to use NSString stringWithFormat:.
while (_data[[NSString stringWithFormat:#"VerticalState%i", i]] != nil) {
Though it would be better to write the loop like this:
int i = 0;
while (1) {
NSString *key = [NSString stringWithFormat:#"VerticalState%i", i];
id value = _dict[key];
if (value) {
// do things
i++;
} else {
break;
}
}

how to load textfield value in to nsmutablearray in string formate in ios

i am trying to load textfield value in to mutable array using add object method but that value is not in string formate.i enter the array structure in below. thank you.
{
ItemUPC = rr001; ------->>>>>in this place
ItemUnits = "";
Itemcost = "";
}
i expected this structure
{
ItemUPC = "rr001"; ------->>>>>in this place
ItemUnits = "";
Itemcost = "";
}
thankyou
Try with this:
[NSString stringWithFormat:#"%#", yourtextField.text];
in both cases it IS actually a string. the problem is that NSLog() only shows the "" signs if it is an empty string or if there are some spaces within the string!
you can check it like this:
for (id object in yourArrayName) {
NSLog(#"%#", [object class]);
}
if the object IS a string it should say something like __NSCFConstantString.

How to format an XLForm row value

I'm using XLForm in my project and want to display the value of one of the rows in a specific format.
The row is "Annual turnover" and I want to display the value entered by the user as a currency - for example, if the user enters 1000000 into the form field, the field should be updated to display this: $ 1,000,000.00
How do I do that?
This is how the form field is initialised in initializeForm():
-(void)initializeForm
{
// Other fields in the form...
// Annual turnover Section
self.section = [XLFormSectionDescriptor formSectionWithTitle:formLabel8AnnualTurnover];
//section.footerTitle = #"Describe new products";
[self.formDescriptor addFormSection:self.section];
// Annual turnover
self.row = [XLFormRowDescriptor formRowDescriptorWithTag:formField8AnnualTurnover rowType:XLFormRowDescriptorTypeText title:nil];
self.row.required = NO;
rowDescriptor8AnnualTurnover = self.row;
[self.section addFormRow:self.row];
// Other fields in the form...
self.form = self.formDescriptor;
}
According to the documentation, I should catch that the user has interacted with the field and then change the value of the field and update the row.
This is how I catch that the user has entered something into the field:
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
[super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
// Get the current form values
formValues = [self.form formValues];
int i = 0;
if ([rowDescriptor.tag hasPrefix:formField8AnnualTurnover]){
NSString *annualTurnoverValue = [(XLFormOptionsObject*)formValues[formField8AnnualTurnover] displayText];
rowDescriptor8AnnualTurnover.value = [NSString stringWithFormat:#"R %#", annualTurnoverValue]; //This breaks.
[self reloadFormRow:rowDescriptor8AnnualTurnover];
}
}
An endless loop was being created by resetting the value of the rowDescriptor in formRowDescriptorValueHasChanged.
I resolved this issue by changing formRowDescriptorValueHasChanged to first check whether or not the rowDescriptor's value already starts with the currency symbol, and only add it if it's missing:
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
[super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
// Get the current form values
formValues = [self.form formValues];
if ([rowDescriptor.tag hasPrefix:formField8AnnualTurnover]){
NSString *annualTurnoverValue = [(XLFormOptionsObject*)formValues[formField8AnnualTurnover] displayText];
if([annualTurnoverValue hasPrefix:#"R "]){
// Do nothing - the value will simply be increased.
}else{
rowDescriptor8AnnualTurnover.value = [NSString stringWithFormat:#"R %#", annualTurnoverValue];
[self reloadFormRow:rowDescriptor8AnnualTurnover];
}
}
}

Extra call to setMarkedText:selectedRange in iOS7?

In my app I have UITextInput implemented so I can handle multi stage key input (Japanese, Chinese keyboards) for my custom text view. I'm noticing in iOS7, when you have some text that is marked, and you tap one of the suggestions above the keyboard to replace it, that setMarkedText:selectedRange is called twice: once where it replaces the marked text with the string selected from the panel above the keyboard (as you'd expect), and once where an empty string is sent as the parameter. In iOS6, it's only called once.
My questions are, is there a reason it's doing this? And how should I adjust my setMarkedText:selectedRange to account for this (listed below):
- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
{
NSRange selectedNSRange = self.textView.selectedTextRange;
NSRange markedTextRange = self.textView.markedTextRange;
if (markedTextRange.location != NSNotFound)
{
if (!markedText)
markedText = #"";
[self.text replaceCharactersInRange:markedTextRange withString:markedText];
markedTextRange.length = markedText.length;
}
else if (selectedNSRange.length > 0)
{
[self.text replaceCharactersInRange:selectedNSRange withString:markedText];
markedTextRange.location = selectedNSRange.location;
markedTextRange.length = markedText.length;
}
else
{
[self.text insertString:markedText atIndex:selectedNSRange.location];
markedTextRange.location = selectedNSRange.location;
markedTextRange.length = markedText.length;
}
selectedNSRange = NSMakeRange(selectedRange.location + markedTextRange.location, selectedRange.length);
self.textView.contentText = self.text;
self.textView.markedTextRange = markedTextRange;
self.textView.selectedTextRange = selectedNSRange;
}
My first instinct is to put an if statement around the contents saying
if markedText != #""
but I'm not sure if I'd be messing up some other cases. Does anyone have any suggestions on how to account for this change??
The guy from DTS recommended this solution:
- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
{
...
if (markedText == nil || markedText.length == 0 )
{
[self unmarkText];
}
}
And it seems to work fine.

Resources