以编程方式返回选项卡栏控制器中的视图

Returning to a view in a Tab Bar Controller Programatically

我有一个 TabBarController,里面有 5 个 ViewController,在其中一个 ViewController 上,我有一组按钮,可以在 TabBarController 之外加载不同的视图控制器。这些是通过 Modal Segue 加载的,但是我遇到的问题是当我尝试返回到它加载的选项卡栏中的视图但没有选项卡栏本身时,我的代码是:

class GreetingsVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func goBack(sender: AnyObject) {

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("anonScreen") as! AnonVC
        self.presentViewController(nextViewController, animated:true, completion:nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

如何才能在按下后退按钮时显示标签栏控制器中的视图控制器?

据我了解,您已经通过标签栏控制器显示了 GreetingsVC presentViewController,并且您想返回到上一个视图(其中一个标签栏视图控制器)

而不是使用 presentViewController,您需要使用 dismissViewControllerAnimated

@IBAction func goBack(sender: AnyObject) {

    dismissViewControllerAnimated(true, completion: nil)
}