2016-10-15 52 views
1

我试图从viewcontroller只有当我的条件得到满足时,才能从secondviewcontroller。我已经从viewcontroller中的按钮连接到2ndviewcontroller。到目前为止,我有:有条件的segue传递数据?

@IBAction func switchView(_ sender: AnyObject) { 
     // if a and b's textfields aren't empty 
     if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!) { 
      a1 = a.text!; 
      b1 = b.text!; 
     }else{ 
      // do something 
     } 

    } 

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { 
    if(identifier == "2ndviewcontroller") { 
     if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!) { 
      return true 
     } 
    } 
    return false 
} 

有了这个,我已经能够使SEGUE只有当A和B的文本字段不为空。这正是我想要的,但我也希望将数据从viewcontroller传递给2ndviewcontroller。

func prepForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "2ndviewcontroller" { 
     let 2ndviewcontroller = segue.destination as! 2ndviewcontroller 
      2ndviewcontroller.c = a 

     } 
    } 
} 

我使用上面的代码将数据从viewcontroller传递到2ndviewcontroller。问题是我不知道如何将它们合并到传递数据中,并且只有在满足条件时才会使其延续。当我有两个函数时,bool函数正确执行,但prepForSegue不传递数据。当我将bool函数注释掉时,prepForSegue会传递数据,但我无法应用制作segue的条件。

编辑:通过使用下面的评论中提供的prepareForSegue方法修复。

+2

你为什么要合并它们?他们是两种不同的方法。一旦决定是否执行segue,另一个允许您根据需要传递数据。如果赛格不应执行,你会传递什么数据? –

+0

当我有两个函数时,布尔函数正确执行,并在条件满足时进行延迟。但是,传递数据的函数不起作用。当我注释掉布尔函数时,我可以很好地传递数据,但是当我点击按钮时,无论条件是否满足,当然我都会继续。我已更新我的问题以澄清。 – Chrine

+2

两者都应该被执行。正确的方法名称是'prepareForSegue',而不是'prepForSegue'。 –

回答

2

正如评论中所述,方法名称应该是prepareForSegue,而不是prepForSegue