Swift Develops Calculator Application

Asked 2 years ago, Updated 2 years ago, 31 views

I am currently developing a calculator application, but I have a question because an error occurred and I cannot proceed.

Enter a description of the image here
Image Description What I want to implement is to show which button was tapped when I tapped this button.

The error is as follows:

Calculator.ViewController buttonTapped:—Unrecognized selector sent to instance 0x7988fea0

It is said that the buttonTapped method is not recognized.
I checked the typographical errors and omissions, so there seems to be no problem.I don't know if I can't recognize it...

Below the source code.
//
// ViewController.swift
// Calculator //

import UIKit

classViewController:UIViewController {

    // Declare a label to display the calculation results
    var resultLabel = UILabel()
    let xButtonCount = 4 // Number of buttons to place in a row
    lettyButtonCount=4
    // Member variable that stores the screen width size
    let screenWidth —Double=Double (UISscreen.mainScreen().bounds.size.width)
    // Vertical screen
    let screenHeight: Double=Double (UISscreen.mainScreen().bound.size.height)
    // margins between buttons
    let buttonMargin=10.0
    // Display calculation results
    var resultArea=0.0

    override func viewDidLoad(){
        super.viewDidLoad()
        // Determine the vertical width of the calculation result display area according to the overall vertical width of the screen
        switch screenHeight {
            case480:
                resultArea=200.0
            case568:
                resultArea=250.0
            case667:
                resultArea=300.0
            case736: 
                resultArea=350.0
            default:
                resultArea=0.0
        }
        // Labels for Calculation Results
        resultLabel.frame = CGRect (x:10, y:30, width:screenWidth-20, height:resultArea-30)

        let buttonLabels = [
            "7","8","9","×",
            "4","5","6","-",
            "1","2","3","+",
            "0", "C", " "", "="
        ]

        for variable=0;y<yButtonCount;y++{
            for varx=0;x<xButtonCount;x++{
                // Computer button creation
                var button = UIButton()
                // Width of button
                varButtonWidth=(screenWidth -(buttonMargin*(Double(xButtonCount)+1))/Double(xButtonCount)
                // Vertical width of button
                varButtonHeight=(screenHeight-resultArea-(buttonMargin*Double(yButtonCount)+1))/Double(yButtonCount)
                // button x coordinates
                varButtonPositionX=(screenWidth-ButtonMargin) / Double(xButtonCount) * Double(x) + ButtonMargin
                // button y coordinates
                varButtonPositionY=(screenHeight-resultArea-buttonMargin)/Double(yButtonCount)*Double(y)+buttonMargin+resultArea
                // button placement, size
                button.frame = CGRect (x:buttonPositionX, y:buttonPositionY, width:buttonWidth, height:buttonHeight)
                // Background
                button.backgroundColor=UIColor.greenColor()
                // Button Label Title
                varButtonNumber = y * xButtonCount + x
                // Index number to remove the button label title
                button.setTitle (buttonLabels [buttonNumber], forState:UIControlState.Normal)
                // Action when tapping the button
                button.addTarget(self, action: "buttonTapped:", forControlEvents:UIControlEvents.TouchUpInside)
                // Button placement
                self.view.addSubview(button)
            }
       }

        // Set calculation result label
        resultLabel.backgroundColor=UIColor.grayColor()
        resultLabel.font=UIFont(name: "Arial", size:50)
        resultLabel.textAlignment=NSTextAlignment.Right
        resultLabel.numberOfLines=4
        resultLabel.text="0"

        // Install calculation result label in view controller class view
        self.view.addSubview(resultLabel)

        // Button tap method
        func buttonTapped (sender:UIButton) {
            US>vartapedButtonTitle: String=sender.titleLabel!.text!
            println("\(tappedButtonTitle) button tapped")
        }
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

ios swift

2022-09-30 16:31

2 Answers

in the buttonTapped method,

vartapedButtonTitle: String=sender.currentTitle!

Why don't you change the statement as follows?

vartapedButtonTitle: String=sender.titleLabel!.text


2022-09-30 16:31

The implementation of the action method buttonTapped(sender:UIButton) is misplaced.

import UIKit

classViewController:UIViewController {

    // Declare a label to display the calculation results
    var resultLabel = UILabel()
    let xButtonCount = 4 // Number of buttons to place in a row
    lettyButtonCount=4
    // Member variable that stores the screen width size
    let screenWidth —Double=Double (UISscreen.mainScreen().bounds.size.width)
    // Vertical screen
    let screenHeight: Double=Double (UISscreen.mainScreen().bound.size.height)
    // margins between buttons
    let buttonMargin=10.0
    // Display calculation results
    var resultArea=0.0

    override func viewDidLoad(){
        super.viewDidLoad()
        // Determine the vertical width of the calculation result display area according to the overall vertical width of the screen
        switch screenHeight {
            case480:
                resultArea=200.0
            case568:
                resultArea=250.0
            case667:
                resultArea=300.0
            case736: 
                resultArea=350.0
            default:
                resultArea=0.0
        }
        // Labels for Calculation Results
        resultLabel.frame = CGRect (x:10, y:30, width:screenWidth-20, height:resultArea-30)

        let buttonLabels = [
            "7","8","9","×",
            "4","5","6","-",
            "1","2","3","+",
            "0", "C", " "", "="
        ]

        for variable=0;y<yButtonCount;y++{
            for varx=0;x<xButtonCount;x++{
                // Computer button creation
                var button = UIButton()
                // Width of button
                varButtonWidth=(screenWidth -(buttonMargin*(Double(xButtonCount)+1))/Double(xButtonCount)
                // Vertical width of button
                varButtonHeight=(screenHeight-resultArea-(buttonMargin*Double(yButtonCount)+1))/Double(yButtonCount)
                // button x coordinates
                varButtonPositionX=(screenWidth-ButtonMargin) / Double(xButtonCount) * Double(x) + ButtonMargin
                // button y coordinates
                varButtonPositionY=(screenHeight-resultArea-buttonMargin)/Double(yButtonCount)*Double(y)+buttonMargin+resultArea
                // button placement, size
                button.frame = CGRect (x:buttonPositionX, y:buttonPositionY, width:buttonWidth, height:buttonHeight)
                // Background
                button.backgroundColor=UIColor.greenColor()
                // Button Label Title
                varButtonNumber = y * xButtonCount + x
                // Index number to remove the button label title
                button.setTitle (buttonLabels [buttonNumber], forState:UIControlState.Normal)
                // Action when tapping the button
                button.addTarget(self, action: "buttonTapped:", forControlEvents:UIControlEvents.TouchUpInside)
                // Button placement
                self.view.addSubview(button)
            }
       }

        // Set calculation result label
        resultLabel.backgroundColor=UIColor.grayColor()
        resultLabel.font=UIFont(name: "Arial", size:50)
        resultLabel.textAlignment=NSTextAlignment.Right
        resultLabel.numberOfLines=4
        resultLabel.text="0"

        // Install calculation result label in view controller class view
        self.view.addSubview(resultLabel)

    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Button tap method (move from viewDidLoad() method to here)
    func buttonTapped (sender:UIButton) {
        US>vartapedButtonTitle: String=sender.titleLabel!.text
        println("\(tappedButtonTitle) button tapped")
    }
}


2022-09-30 16:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.