Hello iPhone
Posted by Topchiyev at 20:06
Here I’m gonna introduce you the paradigm of programming for iPhone using CocoaTouch, Objective C.
The aim is to make a calculator app. By the way, this app is first created for Mac and then ported to iPhone. And that requred just fiew changes. I mean development for both platforms is mostly identical.
Well, I have a
standard program scheme that I use each time I need to test some new for me platform, that’s “Calculator”.
It looks like this:
- We have 1 class, let’s call it MYCalculator.
- We have 2 classwide available strings: currentVal and previousVal. Theese strings are for storing current and previous values and manipulate them during operations. Both this variables are initialized with “0″ string.
- We have an Int32 to store the operator code that we choose by pressing some operator button. Then we’ll use it on Equals button press event. This variable is initiolized with 0 value.
Also we have a Window for storing user interface. And in it:
- We have digit buttons 0 – 9.
- We have operator buttons +,-,x,/.
- We have equals button.
- We have clear button.
- We have a single read only text box or label or something like that.
In code we have to:
- We have a single event processor for digit buttons. It have to get text of sender button and append it to the currentVal string, If currentVal equals “0″ then we simply replace the string by sender text. Then set the text boxs value to currentVals value
- And the last we have a single event processor for operation buttons. First we have to check if the last character of previousVal equals to “.” then we have to remove that char. And if operator variable not equals to 5 then we copy the value of currentVal to previousVal. If operator is between 1 ant 4 then we first execute “=” event processor. It have to get text of sender button and see if it equals to “+” then set the operator variable to 1, if to “-” then 2, if to “x” then to 3, if to “/” then to 4. Then we set the text box value to currentVal value.
- We have an event processor for “=” button. First we look at value of operator variable, and if it’s between 1 and 4 including, then according to that value we execute selected operation (1 is +, 2 is -, 3 is x, 4 is /) using currentVal and previousVal and save it to previousVal. Then we set currentVal to “0″ and operator to 5.
- We have an event processor for clear button. Here we set all variables (currentVal, previousVal operator) to “0″. Then we set the text box value to currentVal value.
The souce code can be downloaded here
Note that this X Code project created using PWNed iPhone temlate profile.
