开发者

string/number from TextField to be used for calculation?

开发者 https://www.devze.com 2022-12-18 03:27 出处:网络
sorry for my question and for the long explanation following here, probably there\'s a simple solution for someone who is experienced in Cocoa and Objective-C, but I\'m just starting few weeks ago and

sorry for my question and for the long explanation following here, probably there's a simple solution for someone who is experienced in Cocoa and Objective-C, but I'm just starting few weeks ago and I can't figure out how to get this thing working, Grrrrrrr!!

OK, let's put it like this, in my window I have the following:

2 TextField(NSTextField) called:

  • blockOffText
  • blockOnText

1 Label(NSTextField) called:

  • flightTimeText

1 button(NSButton) called:

  • updateButton

What I want to do is all about time calculation I guess, get the "start-time" in one TextField and the "end-time" in the other. One is supposed to be the "takeoff-time" and the other the "landing-time" or for example the "duty-startTime" and "duty-endTime"...it's the same! Then I want to calculate the "flight-time" or "duty-time" and show it in the Label.

At the end I will also need to store the value or the time in a database as an integer, the value should be represented by all minutes corresponding to each time, but the database part is not a problem at the moment.

Maybe I can write the number in the TextField without the format but just the number and get the time show-up formatted in some way? I would like to write for example "1245" and "1525" without having the needs to put the ":" between the hours and minutes, then can I get the value formatted "12:45" "15:25" in the TextField in some way? Maybe after pressing the button?

Ok, that is a second problem anyway, my real problem is I need to get the value I wrote in the TextField ("1245" and "1525") to be assigned to some variables in the program that I called "BlockOff" and开发者_如何学Go "BlockOn". I need to transform them in an integer that represent the minutes corresponding to their value..example:

The 1245 will become 765 minutes...(12 times 60 + 45)

and 1525 will become 925 minutes...(15 times 60 + 25)

HOW CAN I DO THIS?

In this way I can use the minutes to calculate the difference to get the flight time or even add flight time to other flight time.

At the moment my program works a little bit differently... like this: If I directly assign the value to the two variables:

int blockOff = 765;

int blockOn = 925;

then I can calculate and show in the two TextField the takeOff and landing time formatted like I want: "12:45" "15:25"...I use other 2 variables to do so:

int oreBlkOff, minBlkOff = 0;

minBlkOff = blockOff % 60;

oreBlkOff = (blockOff - minBlkOff) / 60;

Then I can show the value in the TextField:

[blockOffText setStringValue:[NSString stringWithFormat: @"%d:%d", oreBlkOff, minBlkOff]];

Same with blockOnText and flightTimeText, so there is no problem there, but this is not really what I need right?

HOW CAN I GET THE VALUE OF THE TEXTFIELD AND STORE THE VALUE IN VARIABLES THAT CAN BE USED TO DO CALCULATION?

HOW DO I GET THE FIRST TO DIGITS AND LAST TO DIGITS FROM THE VARIABLES SO I CAN USE THEM AS HOURS AND MINUTES FOR MY CALCULATION?

I WOULD LIKE TO BE ABLE TO TYPE THE VALUE IN THE TEXTFIELD, PRESS "ENTER" OR "RETURN" AND GET THE VALUE ASSIGNED IN THE VARIABLE.

IS THIS THE CORRECT WAY OR I'M JUST GOING THE WRONG WAY IN THIS. IN ANOTHER PROGRAM I DID IN VISUAL BASIC THAT WAS THE WAY I USED.

THANK YOU VERY MUCH IN ADVANCE FOR YOU HELP!

Gianluca


To get the int value from text field you'd use integerValue or floatValue for float. But you should definitely check out the date/time controls in cocoa.


Ok, if I didn't miss the point of your long question... You have a text field with something like @"2345:8743" and you want to get the two numbers in int variables (a=2345, b=8743).

First you need to get these two text representations of your numbers in two NSStrings. You can do it like this:

NSString * yourString = @"2345:8743";
NSString *stringA = [yourString substringToIndex:4]; //stringA = @"2345"
NSString *stringB = [yourString substringFromIndex:5]; //stringB = @"8743";

Now to get the in values, just do:

int a = [stringA intValue];
int b = [stringB intValue];

Let me know if that is helpful to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消