首页 > 解决方案 > 如果 selectedSegmentIndex 不同,则 ViewController 不同

问题描述

我的代码是

@IBAction func pressedButton(_ sender: Any) {  
        self.resignFirstResponder()
        if mySegment.selectedSegmentIndex == 0 {

在这里我需要如果我在ViewControllerA上选择 Segment #0 (并按下我的按钮)然后转到ViewControllerB,如果是 Segment #1 然后是ViewControllerC,如果是 Segment #2 然后是ViewControllerD
是否可以?

标签: iosswiftuisegmentedcontrol

解决方案


是的,您可以在 IB 中为他们设置 ID,如果您使用UINavigationController或呈现,则推送

if mySegment.selectedSegmentIndex == 0 {

     let so = self.storyboard?.instantiateViewController(withIdentifier: "AID")

     self.navigationController?.pushViewController(so!, animated: true)
}
else  if mySegment.selectedSegmentIndex == 1 {

    let so = self.storyboard?.instantiateViewController(withIdentifier: "BID")

    self.navigationController?.pushViewController(so!, animated: true)
}

推荐阅读