在performSegueWithIdentifier之后不调用prepareForSegue

时间:2015-10-21 16:20:45

标签: ios swift

我是iOS开发的新手。有许多重复的问题(抱歉)。但我无法解决这个问题。

调用performSegueWithIdentifier后不会调用prepareForSegue。

show segue标识符命名为' segue1'。 从ViewController按Ctrl键拖动到DetailViewController。

我错过了什么吗?

ViewController.swift:

class ViewController: UIViewController {

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

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

@IBAction func buttonTapped(sender: AnyObject) {
    print("ViewController - buttonTapped()")
    performSegueWithIdentifier("segue1", sender: self)
}

override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
    print("ViewController - performSegueWithIdentifier(): \(identifier)")
}

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    print("ViewController - shouldPerformSegueWithIdentifier()")
    return true
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    print("ViewController - prepareForSegue(): \(segue.identifier!)")
}
}

DetailViewController.swift:

class DetailViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    print("DetailViewController - viewDidLoad()")
}

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

这是screenshot of storyboard

输出(点击按钮后)

ViewController - buttonTapped()
ViewController - performSegueWithIdentifier(): segue1

1 个答案:

答案 0 :(得分:5)

你没有调用超级 - 添加以下内容将消息发送给父类(这将反过来激活segue)

super.performSegueWithIdentifier(identifier, sender: sender)