ios - SWIFT 单击按钮应用程序崩溃

标签 ios swift ibaction sigabrt unrecognized-selector

当我点击按钮(连接到 @IBAction fun Plus(..)..) 时,我的应用程序崩溃并提示:

Thread 1: signal SIGABRT in AppDelegate.Swift file. 

Xcode 是这样说的:

`2015-05-05 00:43:11.165 koka[14911:632850] -[koka.ViewController mainButton:]: unrecognized selector sent to instance 0x7fa4205786b0
2015-05-05 00:43:11.192 koka[14911:632850] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[koka.ViewController mainButton:]: unrecognized selector sent to instance 0x7fa4205786b0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000108615a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a16dbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010861cd1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001085749dc ___forwarding___ + 988
    4   CoreFoundation                      0x0000000108574578 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000108ea9a22 -[UIApplication sendAction:to:from:forEvent:] + 75
    6   UIKit                               0x0000000108fb0e50 -[UIControl _sendActionsForEvents:withEvent:] + 467
    7   UIKit                               0x0000000108fb021f -[UIControl touchesEnded:withEvent:] + 522
    8   UIKit                               0x0000000108eefb68 -[UIWindow _sendTouchesForEvent:] + 735
    9   UIKit                               0x0000000108ef0493 -[UIWindow sendEvent:] + 683
    10  UIKit                               0x0000000108ebcfb1 -[UIApplication sendEvent:] + 246
    11  UIKit                               0x0000000108eca227 _UIApplicationHandleEventFromQueueEvent + 17700
    12  UIKit                               0x0000000108ea523c _UIApplicationHandleEventQueue + 2066
    13  CoreFoundation                      0x000000010854ac91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x0000000108540b5d __CFRunLoopDoSources0 + 269
    15  CoreFoundation                      0x0000000108540194 __CFRunLoopRun + 868
    16  CoreFoundation                      0x000000010853fbc6 CFRunLoopRunSpecific + 470
    17  GraphicsServices                    0x000000010c71ea58 GSEventRunModal + 161
    18  UIKit                               0x0000000108ea8580 UIApplicationMain + 1282
    19  koka                                0x000000010842fa6e top_level_code + 78
    20  koka                                0x000000010842faaa main + 42
    21  libdyld.dylib                       0x000000010a949145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

这是我的代码。有很多不必要的信息,但为了确保,我已经发布了所有代码。您可以看到 @IBAction func Enter(..).. 的主体类似于 @IBAction func Plus(..).. 一个,但不是Plus 函数第一个工作正常。

import UIKit

class ViewController: UIViewController {

    var num = ""
    var typedNum: [Int] = []
    var numArray: [[Int]] = []
    var numSumArray: [[Int]] = []
    var plusFlag = false
    var minusFlag = false
    var resultLabel = "Res: "
    var length: Int = Int()

    @IBOutlet weak var resultField: UILabel!

    @IBAction func Reset(sender: UIButton!) {
        resultLabel = "Res: "
        resultField.text = resultLabel
        num = ""
        plusFlag = false
        minusFlag = false
        numArray = []
        numSumArray = []
    }

    @IBAction func Enter(sender: UIButton!) {
        resultLabel += " = "
        numArray = getNum(num)
        length = numArray.count
        if (length == 1){
            resultLabel += num
        }
        resultField.text = resultLabel
        if (length > 1){
            if plusFlag{
                numSumArray[length-1] = SumOfTwo(numArray[length-2], second: numArray[length-1])
                for i in numSumArray{
                    resultLabel += "\(i)"
                }
            }
            if minusFlag{
            }
        }

        //reset
        resultLabel = "Res: "
        num = ""
        plusFlag = false
        minusFlag = false
        numArray = []
        numSumArray = []
    }

    @IBAction func Minus(sender: UIButton!) {

    }

    @IBAction func Plus(sender: UIButton!) {
        numArray = getNum(num)
        length = numArray.count
        if (length > 1){
            if plusFlag{
                numSumArray[length-1] = SumOfTwo(numArray[length-2], second: numArray[length-1])
                numArray[length-1] = numSumArray[length-1]
                plusFlag = false
            }
            if minusFlag{
            }
        }
        plusFlag = true
        resultLabel += " + "
        resultField.text = resultLabel
        num = ""
    }

    @IBAction func Null(sender: UIButton!) {
        num += "0"
        resultLabel += "0"
        resultField.text = resultLabel
    }

    @IBAction func One(sender: UIButton!) {
        num += "1"
        resultLabel += "1"
        resultField.text = resultLabel
    }

    @IBAction func Two(sender: UIButton!) {
        num += "2"
        resultLabel += "2"
        resultField.text = resultLabel
    }

    @IBAction func Three(sender: UIButton!) {
        num += "3"
        resultLabel += "3"
        resultField.text = resultLabel
    }

    @IBAction func Four(sender: UIButton!) {
        num += "4"
        resultLabel += "4"
        resultField.text = resultLabel
    }

    @IBAction func Five(sender: UIButton!) {
        num += "5"
        resultLabel += "5"
        resultField.text = resultLabel
    }

    @IBAction func Six(sender: UIButton!) {
        num += "6"
        resultLabel += "6"
        resultField.text = resultLabel
    }

    @IBAction func Seven(sender: UIButton!) {
        num += "7"
        resultLabel += "7"
        resultField.text = resultLabel
    }

    @IBAction func Eight(sender: UIButton!) {
        num += "8"
        resultLabel += "8"
        resultField.text = resultLabel
    }

    @IBAction func Nine(sender: UIButton!) {
        num += "9"
        resultLabel += "9"
        resultField.text = resultLabel
    }


    func getNum(num: String) -> [[Int]]{
        var i = 0
        for Character in num{
            let characterString = String(count: 1, repeatedValue: Character)
            typedNum.append(characterString.toInt()!)
            i++
        }
        numArray.append(typedNum)
        return numArray
    }

    func SumOfTwo(first: [Int], second: [Int]) -> [Int]{
        var perenos = 0
        var newValue: Int
        var newArray: [Int] = []
        for (index, value) in enumerate(first){
            newValue = value + second[index]
            perenos = newValue / 10
            newValue %= 10
            newArray[index] = newValue
        }
        return newArray
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        //let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
        //button.addTarget(self, action: "Plus", forControlEvents: UIControlEvents.TouchUpInside)

        // 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.
    }


}

附言我试图将按钮从 Main.storyboard 重新连接到 ViewController.Swift 但它没有帮助。我没有更改 AppDelegate.Swift

最佳答案

终于找到答案了!我删除了以前从 Main.storyboard 连接的 IBActions:View Controller Scene -> Show the Connections Inspector

关于ios - SWIFT 单击按钮应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041220/

相关文章:

iphone - IBAction - 在连接到服务器之前做一些事情

ios - IBAction 函数不会在继承自 UIPageViewControllerDataSource 的类中触发

ios - 如何手动将下一个信号发送到 RxSwift 中的可观察对象?

javascript - 使用 UI 自动化检测设备类型

ios - 在不使用 Segue 的情况下使按钮在单击时显示新 View ?

ios - 当 uiviewcontroller 被取消并且有一个事件的 urlsessiontask 时如何防止崩溃

ios - UITableView - 最后一部分的页脚滚出屏幕底部。如何预防?

ios - 响应 UIViewController 中的表格单元格操作

ios - UIStackView 在 Storyboard 中添加重叠 View

javascript - 在 NativeScript 中显示 PopOver