Cocoa Touch - UISegmentedControl title is not working in IF/ELSE statement - objective-c

I'm trying to write a basic DST converter. I have a segmented control with 3 choices, their titles (surprisingly) are Distance, Speed and Time. I have 2 input text fields and a calculate button, as well as 2 labels for each text field with the type of measurement required and it's units. Making a selection on the segmented control should update the view accordingly. The variables have all been declared as IBOutlets, #property, #synthesize, and the code sits in an IBAction method, which is connected to the segmented control. The following code does not work, am I missing something completely obvious? (NSLog shows the correct title)
NSString *choice;
choice = [dstChoiceSegmentedControl titleForSegmentAtIndex: dstChoiceSegmentedControl.selectedSegmentIndex];
NSLog(#"Choice |%#|", choice);
if (choice == #"Distance") {
firstLabel.text = #"Speed:";
firstUnitsLabel.text = #"kts";
secondLabel.text = #"Time:";
secondUnitsLabel.text = #"hrs";
answerUnitsLabel.text = #"nm";
} else if (choice == #"Speed") {
firstLabel.text = #"Distance:";
firstUnitsLabel.text = #"nm";
secondLabel.text = #"Time:";
secondUnitsLabel.text = #"hrs";
answerUnitsLabel.text = #"kts";
} else if (choice == #"Time") {
firstLabel.text = #"Distance:";
firstUnitsLabel.text = #"nm";
secondLabel.text = #"Speed:";
secondUnitsLabel.text = #"kts";
answerUnitsLabel.text = #"hrs";
}
Thanks for your help (and I hope it's not some silly error that is staring me right in the face)!

You cannot compare strings this way. You need to do:
[choice isEqualToString:#"Distance"];
But if I were you, I'd check for the indicies instead.
edit: To explain it further: what you're doing with choice == #"Distance" is comparing a pointer with a string, which will not work. You need to call the string objects comparing method as shown above.

Related

Getting the name of the variable as a string in GD Script

I have been looking for a solution everywhere on the internet but nowhere I can see a single script which lets me read the name of a variable as a string in Godot 3.1
What I want to do:
Save path names as variables.
Compare the name of the path variable as a string to the value of another string and print the path value.
Eg -
var Apple = "mypath/folder/apple.png"
var myArray = ["Apple", "Pear"]
Function that compares the Variable name as String to the String -
if (myArray[myposition] == **the required function that outputs variable name as String**(Apple) :
print (Apple) #this prints out the path.
Thanks in advance!
I think your approach here might be a little oversimplified for what you're trying to accomplish. It basically seems to work out to if (array[apple]) == apple then apple, which doesn't really solve a programmatic problem. More complexity seems required.
First, you might have a function to return all of your icon names, something like this.
func get_avatar_names():
var avatar_names = []
var folder_path = "res://my/path"
var avatar_dir = Directory.new()
avatar_dir.open(folder_path)
avatar_dir.list_dir_begin(true, true)
while true:
var avatar_file = avatar_dir.get_next()
if avatar_file == "":
break
else:
var avatar_name = avatar_file.trim_suffix(".png")
avatar_names.append(avatar_name)
return avatar_names
Then something like this back in the main function, where you have your list of names you care about at the moment, and for each name, check the list of avatar names, and if you have a match, reconstruct the path and do other work:
var some_names = ["Jim","Apple","Sally"]
var avatar_names = get_avatar_names()
for name in some_names:
if avatar_names.has(name):
var img_path = "res://my/path/" + name + ".png"
# load images, additional work, etc...
That's the approach I would take here, hope this makes sense and helps.
I think the current answer is best for the approach you desire, but the performance is pretty bad with string comparisons.
I would suggest adding an enumeration for efficient comparisons. unfortunately Godot does enums differently then this, it seems like your position is an int so we can define a dictionary like this to search for the index and print it out with the int value.
var fruits = {0:"Apple",1:"Pear"}
func myfunc():
var myposition = 0
if fruits.has(myposition):
print(fruits[myposition])
output: Apple
If your position was string based then an enum could be used with slightly less typing and different considerations.
reference: https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#enums
Can't you just use the str() function to convert any data type to stirng?
var = str(var)

Sales Order Confirmation Report - SalesConfirmDP

I am modifying the SalesConfirmDP class and trying to add the CustVendExternalItem.ExternalItemTxt field into a new field I have created.
I have tried a couple of things but I do not think my syntax was correct i.e I declare the CustVendExternalItem table in the class declaration. But then when I try to insert CustVendExternalItem.ExternalItemTxt into my new field, it does not populate, I guess there must be a method which I need to include?
If anyone has any suggestion it would be highly appreciated.
Thank you in advance.
private void setSalesConfirmDetailsTmp(NoYes _confirmTransOrTaxTrans)
{
DocuRefSearch docuRefSearch;
// Body
salesConfirmTmp.JournalRecId = custConfirmJour.RecId;
if(_confirmTransOrTaxTrans == NoYes::Yes)
{
if (printLineHeader)
{
salesConfirmTmp.LineHeader = custConfirmTrans.LineHeader;
}
else
{
salesConfirmTmp.LineHeader = '';
}
salesConfirmTmp.ItemId = this.itemId();
salesConfirmTmp.Name = custConfirmTrans.Name;
salesConfirmTmp.Qty = custConfirmTrans.Qty;
salesConfirmTmp.SalesUnitTxt = custConfirmTrans.salesUnitTxt();
salesConfirmTmp.SalesPrice = custConfirmTrans.SalesPrice;
salesConfirmTmp.DlvDate = custConfirmTrans.DlvDate;
salesConfirmTmp.DiscPercent = custConfirmTrans.DiscPercent;
salesConfirmTmp.DiscAmount = custConfirmTrans.DiscAmount;
salesConfirmTmp.LineAmount = custConfirmTrans.LineAmount;
salesConfirmTmp.CurrencyCode = custConfirmJour.CurrencyCode;
salesConfirmTmp.PrintCode = custConfirmTrans.TaxWriteCode;
if (pdsCWEnabled)
{
salesConfirmTmp.PdsCWUnitId = custConfirmTrans.pdsCWUnitId();
salesConfirmTmp.PdsCWQty = custConfirmTrans.PdsCWQty;
}
**salesConfirmTmp.ExternalItemText = CustVendExternalItem.ExternalItemTxt;**
if ((custFormletterDocument.DocuOnConfirm == DocuOnFormular::Line)
|| (custFormletterDocument.DocuOnConfirm == DocuOnFormular::All))
{
docuRefSearch = DocuRefSearch::newTypeIdAndRestriction(custConfirmTrans,
custFormletterDocument.DocuTypeConfirm,
DocuRestriction::External);
salesConfirmTmp.Notes = Docu::concatDocuRefNotes(docuRefSearch);
}
salesConfirmTmp.InventDimPrint = this.printDimHistory();
Well, AX cannot guess which record you need, there is a helper class CustVendExternalItemDescription to deal with it:
boolean found;
str externalItemId;
...
[found, externalItemId, salesConfirmTmp.ExternalItemText] = CustVendExternalItemDescription::findExternalItemDescription(
ModuleCustVend::Cust,
custConfirmTrans.ItemId,
custConfirmTrans.inventDim(),
custConfirmJour.OrderAccount,
CustTable::find(custConfirmJour.OrderAccount).CustItemGroupId);
The findExternalItemDescription method returns more information than you need here, but you have to define variables to store it anyway.
Well, the steps to solve this problem are fairly easy and i will try to give you a step by step approach how to solve this problem.
1) Are you initialising CustVendExternalItem properly? Make a record of the same and initialise it as Jan has shown above, then debug your code and see if the value is being initialised in your DP class.
2)If your value is being initialised correctly, but it is not showing up in the report design there can be multiple issues such as:
Overlapping of text boxes.
Insufficient space for the given field
Some report parameter/property not being set correctly which causes
your value not to show up on the report.
Check these one by one and you should end up arriving towards a solution

What happens when If function's result will be wrong?

I am learning objective-c when this thing come and bother me.
I tried to make a program that will return which PDF should be shown when the user input specific things.
My code looks like this
filePath = (userChooseA) ? #"firstFilePath" : #"secondFilePath";
By right if User choose A, the file path will be firstFilePath and vice versa.
However, when I purposely input the non existent file for firstFilePath, the system will directly show secondFilePath regardless user's choice.
My Question is,
why is this happening and how to prevent it?
Thank You,
Regards
That's because this statement is equivalent to:
if(userChooseA)
{
filePath = #"firstFilePath";
}else{
filePath = #"secondFilePath";
}
I assume userChooseA is a BOOL set somewhere else in your code when comparing paths, and it's false if the user chooses B or any other path.
Which means that unless the user chooses A, the file path will be B.
If you want to do something else if neither A or B are chosen, you could write (again assuming UserChooseA and userChooseB are Bool set somewhere else in your code):
int fileSelection = 3;
If (userChooseA) fileSelection = 0;
If (userChooseB) fileSelection = 1;
switch (fileSelection) {
case 0:
filePath = #"firstFilePath";
break;
case 1:
filePath = #"secondFilePath";
break;
default:
... do something else...
break;
}
if this is off base, as suggested in the comments we need to see more code about what type userChooseA is and how it gets set.

Do I have to cast on every line?

I've been teaching myself programming from scratch by creating a simple app in Objective C. Today, I was faced with the issue that I had to write a method that didn't know what type of object it was going to get. With the help of Google, I was delighted to discover something called "casting". :)
I am using casting like so:
- (void)aCustomViewControllerNeedsToChangeStuff:(id)viewController
{
((SpecialViewController *)viewController).aProperty = somethingInteresting;
((SpecialViewController *)viewController).anotherProperty = somethingElse;
((SpecialViewController *)viewController).yetAnotherProperty = moreStuff;
}
Do I have to cast on every line like that, or is there a way I can cast "viewController" once in the scope of the method, to make my code neater?
You can cast your controller to temp variable and use it (also added type check - just in case) :
- (void)aCustomViewControllerNeedsToChangeStuff:(id)viewController
{
if ([viewController isKindOfClass:[SpecialViewController class]]){
SpecialViewController *special = (SpecialViewController *)viewController;
special.aProperty = somethingInteresting;
special.anotherProperty = somethingElse;
special.yetAnotherProperty = moreStuff;
}
}
How about:
- (void)aCustomViewControllerNeedsToChangeStuff:(id)viewController
{
SpecialViewController * controller = (SpecialViewController *)viewController;
controller.aProperty = somethingInteresting;
controller.anotherProperty = somethingElse;
controller.yetAnotherProperty = moreStuff;
}
Use one variable like
SpecialViewController *tempController = (SpecialViewController *)viewController;
than use this variable to access value like
tempController.aProperty

How do get the current line text to cursor from a Silverlight 4 RichTextBox Control

In the Winforms RichTextBox control I have previously used the GetLineFromCharIndex method and the GetFirstCharIndexOfCurrentLine to work out the start and end points of the typed text on he current line.
I am struggling with the new RichTextBox control in Silverlight 4 as there doesn't appear to be equivalent methods. GetPositionFromPoint is available but seems a but clunky.
Cheers.
Updated...I have gone someway to making this work but this requires me to use the Select method of the control, this feels very wrong...
private string GetCurrentLine()
{
TextPointer prevSelStart = richTextBox1.Selection.Start;
Point lineStart = new Point(0, prevSelStart.GetCharacterRect(LogicalDirection.Forward).Y);
TextPointer prevSelEnd = richTextBox1.Selection.End;
TextPointer currentLineStart = richTextBox1.GetPositionFromPoint(lineStart);
//need to find a way to get the text between two textpointers
//other than performing a temporary selection in the rtb
richTextBox1.Selection.Select(currentLineStart, prevSelStart);
string text = richTextBox1.Selection.Text;
//revert back to previous selection
richTextBox1.Selection.Select(prevSelStart, prevSelEnd);
return text;
}
I don't think you can't avoid the selection, it's a proper way to do it (the "selection" is just a logical one), but you can avoid GetPositionFromPoint with TextPointer.GetNextInsertionPosition(LogicalDirection ): Start from richTextBox1.Selection.Start and move towards the beginning of the line (char != '\n')
I've needed to figure out when I was on the top line or bottom line of the RTB. To do this I used the GetCharacterRect methods then compared the tops to see if it was on the last line or first line.
You could do the same and use a text pointer to move through the text and number of times the tops don't match.
Here's code to see if the cursor is on the first or last lines:
private bool IsCursorOnFirstLine()
{
TextPointer contentStart = this.ContentStart;
TextPointer selection = this.Selection.End;
Rect startRect = contentStart.GetCharacterRect(LogicalDirection.Forward);
Rect endRect = selection.GetCharacterRect(LogicalDirection.Forward);
return startRect.Top == endRect.Top;
}
private bool IsCursorOnLastLine()
{
TextPointer start = this.Selection.Start;
TextPointer end = this.ContentEnd;
Rect startRect = start.GetCharacterRect(LogicalDirection.Forward);
Rect endRect = end.GetCharacterRect(LogicalDirection.Backward);
return startRect.Top == endRect.Top;
}