执行segue时的例外情况

时间:2015-12-24 21:01:06

标签: ios xcode swift segue

我使用Swift2和Xcode 7.1

当我点击tableViewCell中存在的collectionCell时,我想触发一个segue 只有我有标识符错误。

然而,我已经说好了我的故事板。

enter image description here enter image description here 错误:

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'Receiver()没有带标识符'look'的segue'

enter image description here

    class PostsCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

    var controller: PostsController!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        controller.perform(data![indexPath.item])

    }

    // ...

}

我的控制器:

class PostsController: UITableViewController {

// ....

override func viewDidLoad() {
    super.viewDidLoad()

    // ...
}

// ...

func perform(post: Post) {
    self.post = post
    self.performSegueWithIdentifier("look", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "look") {
        let viewController:SeePostController = segue.destinationViewController as! SeePostController
        viewController.data = self.post
    }
}

}

1 个答案:

答案 0 :(得分:1)

而不是PostsController().performSegueWithIdentifier("look", sender: self)使用self.performSegueWithIdentifier("look", sender: self)

<强>更新 创建viewcontroller的新实例并调用perform方法。

var controller: PostsController!
controller.perform(data![indexPath.item])