2016-12-26 59 views
0

我有一个视图控制器,有6个按钮,所有这些按钮都会转到具有表视图的另一个视图控制器。根据所按下的按钮,不同的值传递给视图控制器的表视图:值在一个UIAction按钮上传递延迟

@IBAction func englishButton(_ sender: Any) { 
    valueToPass = "English" 
} 


@IBAction func scienceButton(_ sender: Any) { 
    valueToPass = "Science" 
} 

@IBAction func historyButton(_ sender: Any) { 
    valueToPass = "History" 
} 


@IBAction func foriegnLanguageButton(_ sender: Any) { 
    valueToPass = "Foriegn Language" 
} 


@IBAction func mathButton(_ sender: Any) { 
    valueToPass = "Math" 
} 


@IBAction func otherSubjectsButton(_ sender: Any) { 
    valueToPass = "Other Subjects" 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?){ 

    if (segue.identifier == "english") { 
     // initialize new view controller and cast it as your view controller 
     let viewController = segue.destination as! BuyMenuController 
     // your new view controller should have property that will store passed value 
     viewController.passedValue = valueToPass 
    } 
    if (segue.identifier == "science") { 
     // initialize new view controller and cast it as your view controller 
     let viewController = segue.destination as! BuyMenuController 
     // your new view controller should have property that will store passed value 
     viewController.passedValue = valueToPass 
    } 
    if (segue.identifier == "history") { 
     // initialize new view controller and cast it as your view controller 
     let viewController = segue.destination as! BuyMenuController 
     // your new view controller should have property that will store passed value 
     viewController.passedValue = valueToPass 
    } 
    if (segue.identifier == "foriegnLanguage") { 
     // initialize new view controller and cast it as your view controller 
     let viewController = segue.destination as! BuyMenuController 
     // your new view controller should have property that will store passed value 
     viewController.passedValue = valueToPass 
    } 
    if (segue.identifier == "math") { 
     // initialize new view controller and cast it as your view controller 
     let viewController = segue.destination as! BuyMenuController 
     // your new view controller should have property that will store passed value 
     viewController.passedValue = valueToPass 
    } 
    if (segue.identifier == "otherSubjects") { 
     // initialize new view controller and cast it as your view controller 
     let viewController = segue.destination as! BuyMenuController 
     // your new view controller should have property that will store passed value 
     viewController.passedValue = valueToPass 
    } 
} 

这里的问题是,每当我按下scienceButton,这是传递给下一个视图控制器的值是零或之前通过的值。当再次点击时,通过的值是“科学”,就像它假设的那样。这只发生在那一个按钮上,任何想法为什么会发生这种情况? (我试着用if-else if结构来设置它)。

+1

是通过界面生成器中的按钮上的操作来触发segue吗?如果不这样做。在IBAction代码运行之前,segue可能会触发。在设置值通过后,您应该在您的IBAction中执行SegueWithIdentifier。您将需要在InterfaceBuillder中将视图控制器对象中的segue链接起来,并为其指定标识 – Paulw11

+0

Worked!把这个作为答案提交,所以我可以把它标记为这样。 –

回答

1

您不应该将@IBAction方法与直接链接到Interface Builder中的操作的segues结合使用,因为可能在执行函数之前执行segue。

在设置变量后,您应该将segue链接到视图控制器对象,并在您的@IBAction函数中以编程方式执行它。