【问题标题】:Different ViewController if different selectedSegmentIndex如果 selectedSegmentIndex 不同,则 ViewController 不同
【发布时间】:2018-05-14 10:49:46
【问题描述】:

我的代码是

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

如果我在 ViewControllerA 上选择了 Segment #0(然后按下我的按钮),那么我需要在这里,然后转到 ViewControllerB,如果 Segment #1 然后是 ViewControllerC,如果是 Segment #2,则为 ViewControllerD
有可能吗?

【问题讨论】:

  • 是的。这是可能的。你有写正确的条件。只需在这种情况下推动视图控制器。

标签: ios swift uisegmentedcontrol


【解决方案1】:
  1. 首先为每个 viewController 连接一个 segue。 control-从ViewControllerA顶部的viewController图标拖动到另一个ViewController并选择segue类型。

  2. 点击viewControllers之间的segue箭头,并在右侧的Attributes Inspector中设置它的identifier

  3. 为每个 ViewControllerBViewControllerCViewControllerD 重复第 1 步和第 2 步,为它们的 segues 提供唯一 ID,例如 "segueToB""segueToC""segueToD"
  4. 在您的按钮代码中,执行以下操作:

    let idx = mySegment.selectedSegmentIndex
    let segueID = ["segueToB", "segueToC", "segueToD"][idx]
    self.performSegue(withIdentifier: segueID, sender: self)
    

连接两个segue的演示:

【讨论】:

  • 深深感谢您!但也许我对此很傻,因为我是一个女孩。我为 ViewControllerB like that 设置了 segue 的 ID。但是当我从 ViewControllerA 拖到 ViewControllerC 时,ViewControllerA 和 ViewControllerB 之间的箭头消失了。
  • 我刚试了一下,效果很好。确保您从 viewController 图标(顶部最左侧的图标)控制拖动到新的 viewController。您可以通过这种方式连接任意数量的设备。
  • 你添加了 GIF 太可爱了!我的错误是我尝试了连接按钮(不是 ViewController 图标)。现在没问题,但我的应用程序在运行后立即崩溃。如果 AppDelegate.swift class AppDelegate: UIResponder, UIApplicationDelegate Thread 1: signal SIGABRT 我什至创建了新应用程序。只放那个代码。但无论如何。 My ViewController.swift code我连接到both of them
  • 要找出您的应用程序崩溃的位置,请添加异常断点。在 Xcode 的左侧,单击 Breakpoint navigator 图标(将鼠标悬停在它们上方以找到它们的名称)。然后点击左下角的+,在弹出的窗口中选择Exception Breakpoint...
  • 您的 segementedControl 插座似乎未连接到分段控件。从代码中它旁边的圆圈拖动到情节提要中的segmentedControl进行连接。您可能必须打开辅助编辑器才能执行此操作(单击 Xcode 右上角的两个重叠圆圈图标以显示辅助编辑器
【解决方案2】:

是的,如果您使用UINavigationController 或出席,您可以在 IB 中为他们设置 id 并推送

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)
}

【讨论】:

  • 谢谢。不需要“其他”,因为我的细分市场很少。无论如何它都没有用,因为不可能将 ViewControllerA 与 1 个以上的另一个 ViewController 连接
  • @LauraManukyan 有可能,在 navigationController 中嵌入 VC A 并为您想要呈现的任何 VC 设置 id,您可以将 vcs 无限连接到 VC
【解决方案3】:

斯威夫特:

    if(sender.selectedSegmentIndex==0)
     {

       let lvc = self.storyboard?.instantiateViewController(withIdentifier: "cellVC") as! YourViewController1
       self.navigationController?.pushViewController(lvc!, animated: true)
     }

   else 
     {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "cellVC1") as! YourViewController2
        self.navigationController?.pushViewController(vc!, animated: true)
     }

对象:

 if(sender.selectedSegmentIndex==0)
 {
  viewcontroller1 *yourView1=[[viewcontroller alloc]init];
  [self.navigationController pushViewController:yourView1 animated:YES];
 }
 else 
 {
  viewcontroller2 *yourView2=[[viewcontroller2 alloc]init];
  [self.navigationController pushViewController:yourView2 animated:YES];
 }

【讨论】:

    【解决方案4】:

    您应该为通用解决方案创建枚举,而不是使用 if else 条件。

    像这样创建 ViewController 的枚举,

    enum CurrentViewController : Int {
        case ViewControllerB
        case ViewControllerC
        case ViewControllerD
    
        var description : String {
            switch self {
                case .ViewControllerB: return "ViewControllerB"
                case .ViewControllerC: return "ViewControllerC"
                case .ViewControllerD: return "ViewControllerD"
            }
        }
    }
    

    现在转到适当的控制器,你可以这样做,

    let currentViewController = CurrentViewController(rawValue: mySegment.selectedSegmentIndex)
    let viewController = self.storyboard?.instantiateViewController(withIdentifier: (currentViewController?.description)!)
    self.navigationController?.pushViewController(viewController!, animated: true)
    

    以上是所有人的通用解决方案。如有任何疑问,请告诉我。

    【讨论】:

      【解决方案5】:

      可以,只需从 StoryBoard 中设置 ViewController 的 id

      if mySegment.selectedSegmentIndex == 0 {
      
       let vcB = self.storyboard?.instantiateViewController(withIdentifier: "VCB")
       self.navigationController?.pushViewController(vcB, animated: true)
      
      }else  if mySegment.selectedSegmentIndex == 1 {
      
      let vcC = self.storyboard?.instantiateViewController(withIdentifier: "VCB")
      self.navigationController?.pushViewController(vcC, animated: true)
      
      }else  if mySegment.selectedSegmentIndex == 2 {
      
      let vcD = self.storyboard?.instantiateViewController(withIdentifier: "VCD")
      self.navigationController?.pushViewController(vcD, animated: true)
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-25
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多