Я добавил еще один ViewController в свою раскадровку для своего приложения Swift и связал одну из кнопок с первого экрана ViewController на новом экране. Я хочу добавить код в новый контроллер просмотра и создать для него новый класс, но теперь, когда я создаю приложение и нажимаю кнопку, чтобы перейти на новый экран, я получаю сообщение об ошибке «Неизвестный класс MyOwnViewController в интерфейсе Builder файл." MyOwnViewController - это имя класса для этого экрана. Где я ошибся? Вот как я выложил два класса:Ошибка добавления ViewController в Storyboard
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.
}
}
Я создал новый файл Swift и назвал его «MyOwnViewController» и добавил его в проект, вырезал и вставлял весь этот код в новый файл, но я все еще получаю ошибку «завершение с неперехваченным исключением типа NSException» , – hamza1234
Вы все еще получаете сообщение: «Неизвестный класс MyOwnViewController в файле Interface Builder.»? Если нет, то вы имеете дело с другой проблемой (это хорошо!). Итак, каково новое сообщение об ошибке? Вы должны получать больше деталей, чем указано. –
Правильно, я все еще получаю это сообщение. В нем также говорится: «Завершить приложение из-за неотображенного исключения« NSUnknownKeyException »по этой причине,« этот класс не является ключевым для кодирования для ключа showOption1 ». – hamza1234