UIButton内部的UISegmentedControl动作

时间:2016-10-19 14:21:53

标签: ios swift xcode uibutton uisegmentedcontrol

我正在继续我的计算程序,以提高我在Swift代码中的技能。现在我有一个问题:我希望UISegmentedControl选择我的操作的操作符(+, - ecc ..),当选择一个时,UIButton func根据UISegmentedControl的决定计算值。

这是代码,但无效。

谢谢, 利玛

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var firstNumber: UITextField!
@IBOutlet weak var secondNumber: UITextField!
@IBOutlet weak var resultButton: UIButton!
@IBOutlet weak var resultNumber: UILabel!
@IBOutlet weak var operatorSelector: UISegmentedControl!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


}

override func didReceiveMemoryWarning() {

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


@IBAction func operatorSelectorChanged(_ sender: UISegmentedControl) {
    switch operatorSelector.selectedSegmentIndex {
    case 0:
        @IBAction func resultFunction(_ sender: AnyObject) {
            if let firstNumberConv :Int = Int(firstNumber.text!), let secondNumberConv :Int = Int(secondNumber.text!) {
                let result = firstNumberConv + secondNumberConv
                resultNumber.text = "\(result)"
            } else { resultNumber.text = "Inserire solo valori numerici"}
        }


    case 1:
        @IBAction func resultFunction(_ sender: AnyObject) {
            if let firstNumberConv :Int = Int(firstNumber.text!), let secondNumberConv :Int = Int(secondNumber.text!) {
                let result = firstNumberConv - secondNumberConv
                resultNumber.text = "\(result)"
            } else { resultNumber.text = "Inserire solo valori numerici"}
        }

    default:
        break;

    }

}



}

2 个答案:

答案 0 :(得分:0)

尝试删除切换案例中@IBAction func ...的行。

我认为你的@IBAction func operatorSelectorChanged应该是这样的:

@IBAction func operatorSelectorChanged(_ sender: UISegmentedControl) {
    switch operatorSelector.selectedSegmentIndex {
    case 0:
        if let firstNumberConv = Int(firstNumber.text!), let secondNumberConv = Int(secondNumber.text!) {
            let result = firstNumberConv + secondNumberConv
            resultNumber.text = "\(result)"
        } else { resultNumber.text = "Inserire solo valori numerici"}


    case 1:
        if let firstNumberConv = Int(firstNumber.text!), let secondNumberConv = Int(secondNumber.text!) {
            let result = firstNumberConv - secondNumberConv
            resultNumber.text = "\(result)"
        } else { resultNumber.text = "Inserire solo valori numerici"}

    default:
        break;

    }

}

答案 1 :(得分:0)

问题的一个可能解决方案是定义一个IBAction来计算加/减两个数的结果

cmake_minimum_required(VERSION 2.8)
set (PROJ_NAME YourAppName)
project(${PROJ_NAME})
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

FIND_PACKAGE(OpenCV)

set(folder_source   main.cpp
                    )

set(folder_header   main.h
                    )

SOURCE_GROUP("Source Files" FILES ${folder_source})
SOURCE_GROUP("Header Files" FILES ${folder_header})
ADD_EXECUTABLE(${PROJ_NAME} ${folder_source} ${folder_header})
TARGET_LINK_LIBRARIES(${PROJ_NAME} ${OpenCV_LIBS} )

然后,您可以将此操作绑定到用户界面中发送的事件:例如,文本字段的@IBAction func calculate(_ sender: AnyObject) { if let a = Int(firstNumber.text!), let b = IntsecondNumber.text!) { switch operatorSelector.selectedSegmentIndex { case 0: resultNumber.text = "\(a+b)" case 1: resultNumber.text = "\(a-b)" default: print("❗️unknown operator selected") } } } 事件(firstNumber和secondNumber)以及分段控件的Editing Changed(operatorSelector)

我创建了一个示例项目来说明这一点:https://github.com/dev1an/calculator

如何将多个事件绑定到一个IBAction

在代码中写下IBAction(就像我上面写的计算操作一样)。转到故事板并打开辅助编辑器( Alt Cmd Enter )。在界面构建器中选择UI元素(如文本字段)。打开Connections Inspector( Alt Cmd 6 )并从发送事件旁边的圆圈(例如编辑更改)拖动到您的IBAction代码