我为我的Swift应用程序添加了另一个ViewController到我的Storyboard,并将第一个ViewController屏幕中的一个按钮链接到新屏幕。我想将代码添加到新的View Controller中,并为它创建一个新类,但是现在当我构建应用程序并单击按钮转到新屏幕时,我收到一条错误,上面写着" Unknown class MyOwnViewController在Interface Builder文件中。" MyOwnViewController是该屏幕的Class名称。我哪里出错了?以下是我如何列出这两个课程的内容:
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.
}
}
答案 0 :(得分:0)
你的MyOwnViewController' class嵌套在' ViewController'类。您应该选择' MyOwnViewController'的所有代码,将其剪切,然后将其粘贴到自己的文件中。