【问题标题】:Error adding ViewController to Storyboard将 ViewController 添加到 Storyboard 时出错
【发布时间】:2014-12-21 03:43:27
【问题描述】:

我在我的 Swift 应用程序的 Storyboard 中添加了另一个 ViewController,并将其中一个按钮从第一个 ViewController 屏幕链接到新屏幕。我想将代码添加到新的视图控制器,并为它创建了一个新类,但是现在当我构建应用程序并单击按钮转到新屏幕时,我收到一条错误消息:“Interface Builder 中的未知类 MyOwnViewController文件。” MyOwnViewController 是该屏幕的类的名称。我哪里做错了?以下是我对这两个类的布局:

import UIKit

class ViewController: UIViewController {

@IBOutlet var showOption1: UILabel!

@IBOutlet var showOption2: UILabel!

@IBOutlet weak var button1: UIButton!

@IBOutlet weak var button2: UIButton!

lazy var buttons: [UIButton] = [self.button1, self.button2]

var voteCount: PFObject?

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

            var voteCount1 = PFObject(className: "VoteCount")
            voteCount1["choices"] = 2
            voteCount1["votes"] = Int()
            voteCount1["votes2"] = Int()
            voteCount1["optionName"] = String()
            voteCount1["optionName2"] = String()
            voteCount1["objectId"] = String()
            voteCount1["pollNumber"] = Int()

            var voteCount2 = PFObject(className: "VoteCount2")
            voteCount2["choices"] = 3
            voteCount2["votes"] = Int()
            voteCount2["votes2"] = Int()
            voteCount2["votes3"] = Int()
            voteCount2["optionName"] = String()
            voteCount2["optionName2"] = String()
            voteCount2["optionName3"] = String()

            var voteCount3 = PFObject(className: "VoteCount3")
            voteCount3["choices"] = 4
            voteCount3["votes"] = Int()
            voteCount3["votes2"] = Int()
            voteCount3["votes3"] = Int()
            voteCount3["votes4"] = Int()
            voteCount3["optionName"] = String()
            voteCount3["optionName2"] = String()
            voteCount3["optionName3"] = String()
            voteCount3["optionName4"] = String()

            var query = PFQuery(className: "VoteCount")
            query.countObjectsInBackgroundWithBlock {
                (count: Int32, error: NSError!) -> Void in
                if error == nil {
                    let randNumber = Int(arc4random_uniform(UInt32(count)))
                    query.whereKey("pollNumber", equalTo: randNumber)
                    query.getFirstObjectInBackgroundWithBlock {
                        (voteCount1: PFObject!, error: NSError!) -> Void in
                        if error != nil {
                            NSLog("%@", error)
                        } else {
                            self.voteCount = voteCount1
                            let votes = voteCount1["votes"] as Int
                            let votes2 = voteCount1["votes2"] as Int
                            let option1 = voteCount1["optionName"] as String
                            let option2 = voteCount1["optionName2"] as String
                            self.showOption1.text = "\(option1)"
                            self.showOption2.text = "\(option2)"
                        }
                    }
                } else {
                    println("error \(error)")
                }
            }

}

@IBOutlet weak var pollResults: UILabel!

@IBAction func addVote1(sender: AnyObject) {
    for button in self.buttons {
        button.enabled = false
    }
    var query = PFQuery(className: "VoteCount")
    query.getFirstObjectInBackgroundWithBlock {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            self.voteCount = voteCount1
            voteCount1.incrementKey("votes")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults.text = "\(votes)                                                       \(votes2)"
        }
        }
    }

@IBOutlet weak var pollResults2: UILabel!

@IBAction func addVote2(sender: AnyObject) {
    for button in self.buttons {
        button.enabled = false
    }
    var query = PFQuery(className: "VoteCount")
    query.getFirstObjectInBackgroundWithBlock {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            self.voteCount = voteCount1
            voteCount1.incrementKey("votes2")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults2.text = "\(votes)                                                       \(votes2)"
        }
        }

}


@IBAction func goPressed(sender: UIButton) {
    let segues = ["pushTwo1"]
    let index = Int(arc4random_uniform(UInt32(segues.count)))
    let segueName = segues[index]
    self.performSegueWithIdentifier(segueName, sender: self)
}


class MyOwnViewController: UIViewController {

    @IBOutlet var showOption3: UILabel!

    @IBOutlet var showOption4: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        var voteCount1 = PFObject(className: "VoteCount")
        voteCount1["choices"] = 2
        voteCount1["votes"] = Int()
        voteCount1["votes2"] = Int()
        voteCount1["optionName"] = String()
        voteCount1["optionName2"] = String()
        voteCount1["objectId"] = String()
        voteCount1["pollNumber"] = Int()

        var query = PFQuery(className: "VoteCount")
        query.countObjectsInBackgroundWithBlock {
            (count: Int32, error: NSError!) -> Void in
            if error == nil {
                let randNumber = Int(arc4random_uniform(UInt32(count)))
                query.whereKey("pollNumber", equalTo: randNumber)
                query.getFirstObjectInBackgroundWithBlock {
                    (voteCount1: PFObject!, error: NSError!) -> Void in
                    if error != nil {
                        NSLog("%@", error)
                    } else {
                        let votes = voteCount1["votes"] as Int
                        let votes2 = voteCount1["votes2"] as Int
                        let option1 = voteCount1["optionName"] as String
                        let option2 = voteCount1["optionName2"] as String
                        self.showOption3.text = "\(option1)"
                        self.showOption4.text = "\(option2)"
                    }
                }
            } else {
                println("error \(error)")
            }
        }


    }

}

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

}

【问题讨论】:

    标签: ios xcode swift


    【解决方案1】:

    您的“MyOwnViewController”类嵌套在“ViewController”类中。您应该选择“MyOwnViewController”的所有代码,将其剪切并粘贴到它自己的文件中。

    【讨论】:

    • 我创建了一个新的 Swift 文件并将其命名为“MyOwnViewController”并将其添加到项目中,将所有代码剪切并粘贴到新文件中,但我仍然收到错误“以未捕获的方式终止NSException 类型的异常”。
    • 您是否仍然收到消息:“Interface Builder 文件中的未知类 MyOwnViewController。”?如果不是,那么您正在处理一个不同的问题(这很好!)。那么新的错误信息是什么?你应该得到比你所说的更多的细节。
    • 没错,我仍然收到该消息。它还说,“由于未捕获的异常'NSUnknownKeyException'而终止应用程序”,原因是“这个类不符合键showOption1的键值编码。”
    • 打开故事板,然后打开连接检查器。选择情节提要中的每个视图控制器,寻找定义名为 showOption1 的连接的视图控制器。删除该连接。
    • 如果我真的想在我的新视图控制器中添加 IBOutlets,我需要做什么?那就是 showOption1 是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2012-12-19
    • 2012-04-13
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多