在IBAction之前调用prepareForSegue

时间:2014-08-28 13:22:11

标签: uiimageview swift ibaction uitapgesturerecognizer

UIImageView UITapGestureRecognizer。 我想在点击图像时向另一个VC发送一些信息。但prepareForSegue中的代码在任何IBActionUITapGestureRecognizer)之前调用,因此我无法将任何数据放入prepareForSegue的变量中。我怎么能这样做?

所以我有:

var tempVar: NSString = ""

...

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
   if(segue.identifier == "second")
   {
       let secondVC = segue.destinationViewController as secondViewController
       secondVC.someVar = tempVar
   }
}

...

@IBAction func imageTapped(sender: UITapGestureRecognizer)
{
   tempVar = "someText"
   self.navigationController.pushViewController(self.storyboard.instantiateViewControllerWithIdentifier("secondStoryBoardName") as secondViewController, animated: true)
}

2 个答案:

答案 0 :(得分:1)

在我的评论中:

试试这个(在你的imagetapped函数中)

self.performSegueWithIdentifier("second", sender: self)

答案 1 :(得分:0)

你必须做出选择。


如果您想/可以使用segue链接两个viewControllers,则必须使用performSegueWithIdentifier:sender:。然后,prepareForSegue:sender:将帮助您设置新的viewController。

你的代码有一个segue:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
   if(segue.identifier == "second") {
       let secondVC = segue.destinationViewController as secondViewController
       secondVC.someVar = tempVar
   }
}

@IBAction func imageTapped(sender: UITapGestureRecognizer) {
   tempVar = "someText"
    self.performSegueWithIdentifier("second", sender: nil)
    //No need to give your secondViewController an identifier ("secondStoryBoardName")
}

如果您使用pushViewController:animated:,则不会使用任何segue,prepareForSegue:sender:中的代码也不会被调用。

你的代码没有segue:

@IBAction func imageTapped(sender: UITapGestureRecognizer) {
   tempVar = "someText" 
   self.navigationController.pushViewController(self.storyboard.instantiateViewControllerWithIdentifier("secondStoryBoardName") as secondViewController, animated: true)
}