在swift中隐藏UIViewController(屏幕)

时间:2015-06-17 10:09:55

标签: swift uiviewcontroller uitabbarcontroller segue xcode6.3

enter image description here

如您所见,屏幕流程如下:

点击'按钮2'屏幕A',应用程序显示'屏幕B'来自同一个有标签栏的故事板。 点击“红色”标签后,屏幕B',应用程序显示“绿色”按钮'(右上角)。 点击“绿色”按钮'屏幕B',应用程序显示故事板2的屏幕C'

我需要回到屏幕B' '故事板1'点击屏幕C'的后退按钮后,无论来自哪个屏幕C'已经出现了'屏幕C' '故事板2'应该从上到下消失。

是否可以这样做,如果是,那么如何?

2 个答案:

答案 0 :(得分:5)

是的,您可以隐藏UIViewController,因为UIViewController只是正常的UIView

因此,只需为@IBOutlet创建UIView并使用.hidden方法即可 像这样:

 @IBOutlet weak var mainview: UIView!
 override func viewDidLoad() {
    super.viewDidLoad()

   mainview.hidden = true
}

修改
正确指定您想要实现的目标。如果您的显示viewController来自屏幕A或B,直到应该有一些条件或者ViewController需要一些输入值(字符串,整数等)来处理它,这没关系。

<强> EDIT2
请参阅此答案How to check which segue was used

<强> EDIT3
根据您的编辑答案(移至ScreenC到ScreenB,无论screenC是来自ScreenA还是ScreenB),最好使用三个单独的3个故事板并根据需要调用目标ViewController。

此处是ViewController类的代码(移动 ScreenA - &gt; ScreenB ScreenA - &gt; ScreenC

@IBAction func buttonAisClick(sender: AnyObject) {
// move to ScreenC directly
    var moveToNextVC:ViewController3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3

    self.presentViewController(moveToNextVC, animated: true, completion: nil)
}


@IBAction func ButtomBisClick(sender: AnyObject) {
// move to ScreenB
    var moveToNextVC:ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2

    self.presentViewController(moveToNextVC, animated: true, completion: nil)
}

ViewController2类的代码(从 ScreenB - &gt; ScreenC 移动)

@IBAction func ScreenBbuttonIsClick(sender: AnyObject) {
// move to ScreenC
    var moveToNextVC:ViewController3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3

    self.presentViewController(moveToNextVC, animated: true, completion: nil)

}

ViewController3类的代码(从 ScreenC移动 - &gt; ScreenB

@IBAction func ScreenCbuttonIsclick(sender: AnyObject) {
    var moveToNextVC:ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2

    self.presentViewController(moveToNextVC, animated: true, completion: nil)
}

答案 1 :(得分:-4)

我实现了segue并添加动画并且它可以工作。