1
我遇到一個奇怪的問題。我正在嘗試將一個對象從ViewControllerA
傳遞到CreateInvitationViewController
。Swift中在視圖控制器之間傳遞對象
在ViewControllerA
我有下面的代碼:
func btnPassedRequestTouched(sender:UIButton!)
{
print("button passed request touched")
// pass request
self.performSegueWithIdentifier("createInvitationSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "createInvitationSegue" {
let createInvitationView = segue.destinationViewController as! CreateInvitationViewController
let invitation = invitations[self.carousel.currentItemIndex]
createInvitationView.invitation = invitation
}
}
我把一個斷點在這條線:createInvitationView.invitation = invitation
,我可以看到這個對象存在。
在ViewControllerB
我有以下代碼:
class CreateInvitationViewController: UIViewController {
var invitation = Invitation()
override func viewDidLoad() {
super.viewDidLoad()
// if invitation is set, then use it to populate fields
if let _ = self.invitation.id {
invitationText.hidden = false
invitationText.text = self.invitation.note
}
}
我使用,我所做的SEGUE Show
行動,它看起來像這樣: 這是我得到的錯誤:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyProject.CreateInvitationViewController 0x156365810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postRequest.'
你有什麼建議,爲什麼發生這種情況?
非常感謝,這救了我:) – bla0009