-1

.xib 文件(界面)显示身份检查器选项卡我是 Xcode 和 Swift 的新手,并试图运行一个简单的乘法游戏应用程序。但是,当我尝试在模拟器上运行测试时,收到 SIGABRT 信号错误,并且应用程序无法加载。我不知道如何解决这个问题,请帮忙!我已经尝试了一切,但似乎没有任何效果。注意:我没有使用故事板,并且已经从项目文件夹中删除了 main.storyboard 文件,重点是 xib 文件(我使用的是 Xcode 10.3)

我收到的错误是:

由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类与键 inputField 的键值编码不兼容。”

// This is my AppDelegate. I receive the SIGABRT signal on the third line (class AppDelegate). 


import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var mainVC: MainViewController?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        mainVC = MainViewController(nibName: "MainViewController", bundle:nil)

        let frame = UIScreen.main.bounds
        window = UIWindow(frame:frame)

        window!.rootViewController = mainVC
        window!.makeKeyAndVisible()

        return true
    }

}

单独的文件

//MainViewController.swift, created with MainViewController.xib

import UIKit

class MainViewController: UIViewController
{   
@IBOutlet weak var number1:UILabel?
@IBOutlet weak var number2:UILabel?
@IBOutlet weak var timeLabel:UILabel?
@IBOutlet weak var scorecounter:UILabel?
@IBOutlet weak var inputField:UITextField?

var score:Int = 0
var timer:Timer?
var seconds:Int = 60

override func viewDidLoad()
{
    super.viewDidLoad()
    setRandomNumberLabel1()
    setRandomNumberLabel2()
    updateScoreLabel()

    inputField?.addTarget(self, action: #selector(textFieldDidChange(textField:)), for:UIControl.Event.editingChanged)
}

func updateScoreLabel()
{
    scorecounter?.text = "\(score)"
}

func updateTimeLabel()
{
    if(timeLabel != nil)
    {
        let min:Int = (seconds / 60) % 60
        let sec:Int = seconds % 60

        let min_p:String = String(format: "%02d", min)
        let sec_p:String = String(format: "%02d", sec)

        timeLabel!.text = "\(min_p):\(sec_p)"
    }
}

func setRandomNumberLabel1()
{
    number1?.text = generateRandomString()
}

func setRandomNumberLabel2()
{
    number2?.text = generateRandomString()
}

@objc func textFieldDidChange(textField:UITextField)
{
    if inputField?.text?.count ?? 0 < 1
    {
        return
    }

    if  let number1_text    = number1?.text,
        let number2_text    = number2?.text,
        let input_text      = inputField?.text,
        let number1 = Int(number1_text),
        let number2 = Int(number2_text),
        let input   = Int(input_text)
    {
        print("Comparing: \(input_text) == \(number1_text) times \(number2_text)")

        if(input == number1 * number2)
        {
            print("Correct!")

            score += 1
        }
        else
        {
            print("Incorrect!")

            score -= 1
        }
    }

    setRandomNumberLabel1()
    setRandomNumberLabel2()
    updateScoreLabel()
    updateTimeLabel()

    if(timer == nil)
    {
        timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector:#selector(onUpdateTimer), userInfo:nil, repeats:true)
    }
}

@objc func onUpdateTimer() -> Void
{
    if(seconds > 0 && seconds <= 60)
    {
        seconds -= 1

        updateTimeLabel()
    }
    else if(seconds == 0)
    {
        if(timer != nil)
        {
            timer!.invalidate()
            timer = nil

            let alertController = UIAlertController(title: "Game Over!", message: "Time's up! You got a score of: \(score) points. Good Job!", preferredStyle: .alert)

            let restartAction = UIAlertAction(title: "Restart", style: .default, handler: nil)
            alertController.addAction(restartAction)

            self.present(alertController, animated: true, completion: nil)

            score = 0
            seconds = 60

            updateTimeLabel()
            updateScoreLabel()
            setRandomNumberLabel1()
            setRandomNumberLabel2()
        }
    }
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}

func generateRandomString() -> String
{
    var result:String = ""

    for _ in 1...1
    {
        let digit = Int.random(in: 1..<10)

        result += "\(digit)"
    }

    return result
}
}
4

1 回答 1

0

将您的网点更改为非可选:

@IBOutlet weak var number1: UILabel!
@IBOutlet weak var number2: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var scorecounter: UILabel!
@IBOutlet weak var inputField: UITextField!
  1. 在 Storyboard 中打开 viewController 并选择您的控制器:

在此处输入图像描述

  1. 检查右侧的菜单。它应该已更改为选择最后一个图标,一个箭头。您的网点应该在那里:

在此处输入图像描述

  1. 将您的插座连接到视图上的每个元素。您可以通过单击每个元素上的空单选按钮并拖动光标来执行此操作:

在此处输入图像描述

  1. 再次构建并运行该项目。

如果您在第 2 步中看不到插座,则您可能对您的班级有错误的引用。第1步之后,点击右侧菜单中的按钮:

在此处输入图像描述

如果类字段与您的控制器不匹配,请更改为它。根据您的代码,您的代码应该是MainViewController. 设置它并返回到第 2 步。

于 2019-10-11T14:41:26.803 回答