In this article, iOS application development team is sharing DIY tips to create a universal app- simple calculator. It will have all common features like addition, subtraction, multiplication, and division. You can make your own calculator by following this tutorial.
In this tutorial we will create a universal app - simple calculator, with addition, subtraction, multiplication and division functionality.
Starts with creating one single view application with product name “Swift Calculator”. In language field select swift language and devices field select universal.
To design user interface open Main.storyboard file. Add one label on top of the main view to display the inputs by user. Create required outlet for label as shows in below screen shot.
Next, add buttons for one to nine digits, plus, minus, multiplication, division, equal to (=) sign and C (Cancel/back press/delete) button in main view, set respective titles and create required IBAction as shown in below screen shots.
In ViewController.swift file, declare required variables and set borderWidth, borderColor and cornerRadius for label in viewDidLoad method, as shown in below screenshot.
NumberTap method: This method will be called when user press any number button. In this method keep appending the digits entered by user to our label. isNextType is the flag which identified whether the input is number or any operator (+, -, *, /, =)
CalculationClick method: stores operation (addition or subtraction or multiplication or division) value and firstNumber value.
BtnEqualClick method: Sets result in label using operation selected by user and first and second number inputs done by user.
BtnCancelClick method: This method removes last digit entered by user from label value, a kind of backpress or delete operation.
Please check below screen shot for more info.
Now build and run project. If everything is correct, your app should run properly and you will see below output. Try doing some calculations yourself to verify you implemented operations correctly.
You can share your thoughts related to this DIY tutorial with iOS application development team that has shared this article with you. If you have any doubt or anything is not clear to you, ask them right away and wait for their response.