Different ViewController if different selectedSegmentIndex

六眼飞鱼酱① 提交于 2019-12-26 08:44:04

问题


My code is

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

Here I need if I selected Segment #0 on ViewControllerA (and press my button) then go to ViewControllerB, if Segment #1 then ViewControllerC, if Segment #2 then ViewControllerD.
Is it possible?


回答1:


  1. First wire a segue for each viewController. control-drag from the viewController icon at the top of ViewControllerA to another ViewController and select the segue type.

  2. Click on the segue arrow between the viewControllers, and set its identifier in the Attributes Inspector on the right.

  3. Repeat steps 1 and 2 for each ViewControllerB, ViewControllerC and ViewControllerD giving their segues unique id's such as "segueToB", "segueToC" and "segueToD".
  4. In your button code, do the following:

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

Demo of connecting two segues:




回答2:


Yes you can set id for them in IB and push if you use UINavigationController or present

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



回答3:


Swift:

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

ObjC:

 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:


You should create enum for generic solution instead of using if else condition.

Create enum of your ViewController like this,

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

Now go to appropriate controller, you can do this,

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

Above is generic solution for all. Let me know in case of any queries.




回答5:


Possible ,Simply set the id of ViewController from StoryBoard

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)

}


来源:https://stackoverflow.com/questions/50328507/different-viewcontroller-if-different-selectedsegmentindex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!