我的代码是
@IBAction func pressedButton(_ sender: Any) {
self.resignFirstResponder()
if mySegment.selectedSegmentIndex == 0 {
如果我在 ViewControllerA 上选择了段#0(并按下我的按钮),然后转到 ViewControllerB ,如果是段#1,那么我需要 ViewControllerC ,如果是#2段,那么 ViewControllerD 。
有可能吗?
答案 0 :(得分:0)
是的,您可以在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)
}
答案 1 :(得分:0)
夫特:
/usr/share/cumulocity-rpi-agent/cfg/cumulocity.properties
ObjC:
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)
}
答案 2 :(得分:0)
您应该为通用解决方案创建枚举,而不是使用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)
以上是所有人的通用解决方案。如果有任何疑问,请告诉我。
答案 3 :(得分:0)
首先为每个viewController连接一个segue。 control -drag从ViewControllerA
顶部的viewController图标到另一个ViewController
并选择segue类型。
单击viewControllers之间的segue箭头,并在右侧的 Attributes Inspector 中设置标识符。
ViewControllerB
,ViewControllerC
和ViewControllerD
重复步骤1和2,为其分段提供唯一ID,例如"segueToB"
,"segueToC"
和"segueToD"
。在按钮代码中,执行以下操作:
let idx = mySegment.selectedSegmentIndex
let segueID = ["segueToB", "segueToC", "segueToD"][idx]
self.performSegue(withIdentifier: segueID, sender: self)
连接两个segues的演示:
答案 4 :(得分:0)
可能,只需从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)
}