用 NavigationViewController 快速呈现 ViewController

我有一个系统“ NavigationViewController-> MyViewController”,我想以编程方式在第三视图控制器中显示 MyViewController。问题是我没有在 MyViewController 中显示导航栏。你能帮我吗?

var VC1 = self.storyboard.instantiateViewControllerWithIdentifier("MyViewController") as ViewController
self.presentViewController(VC1, animated:true, completion: nil)
115226 次浏览

调用 presentViewController表示现有导航栈之外的视图控制器 模式上; 它不包含在 UINavigationController 或任何其他控制器中。如果希望新的视图控制器具有导航栏,则有两个主要选项:

选项1。将新的视图控制器推到现有的导航堆栈上,而不是模式化地显示它:

let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
self.navigationController!.pushViewController(VC1, animated: true)

选项2. 将新的视图控制器嵌入到新的导航控制器中,并模态地显示新的导航控制器:

let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.present(navController, animated:true, completion: nil)

请记住,这个选项不会自动包含一个“后退”按钮。你必须自己建立一个封闭的机制。

哪一个最适合你是一个人性化界面设计的问题,但是通常什么是最有意义的是很清楚的。

我的导航栏没有显示,所以我在 Swift 2 iOS9中使用了以下方法

let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as! Dashboard


// Creating a navigation controller with viewController at the root of the navigation stack.
let navController = UINavigationController(rootViewController: viewController)
self.presentViewController(navController, animated:true, completion: nil)

SWIFT 3

let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
let navController = UINavigationController(rootViewController: VC1)
self.present(navController, animated:true, completion: nil)

我使用了 UIViewController 的一个扩展和一个 struct 来确保我当前的视图是从收藏夹中显示出来的

1. 全局 Bool 的结构

struct PresentedFromFavourites {
static var comingFromFav = false}

2. UIVeiwController 扩展: 通过“ stefandouganhyde-Option 2”模式化地提出第二个选项,并解决了后面的问题

extension UIViewController {
func returnToFavourites()
{
// you return to the storyboard wanted by changing the name
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let mainNavigationController = storyBoard.instantiateViewController(withIdentifier: "HomeNav") as! UINavigationController
// Set animated to false
let favViewController = storyBoard.instantiateViewController(withIdentifier: "Favourites")
self.present(mainNavigationController, animated: false, completion: {
mainNavigationController.pushViewController(favViewController, animated: false)
})


}
// call this function in viewDidLoad()
//
func addBackToFavouritesButton()
{
if PresentedFromFavourites.comingFromFav
{
//Create a button
// I found this good for most size classes
let buttonHeight = (self.navigationController?.navigationBar.frame.size.height)! - 15
let rect = CGRect(x: 2, y: 8, width: buttonHeight, height: buttonHeight)
let aButton = UIButton(frame: rect)
// Down a back arrow image from icon8 for free and add it to your image assets
aButton.setImage(#imageLiteral(resourceName: "backArrow"), for: .normal)
aButton.backgroundColor = UIColor.clear
aButton.addTarget(self, action:#selector(self.returnToFavourites), for: .touchUpInside)
self.navigationController?.navigationBar.addSubview(aButton)
PresentedFromFavourites.comingFromFav = false
}


}}

接受的答案是伟大的。这不是答案,但只是一个例子的问题。

我呈现的视图控制器是这样的:

内部 vc1:

func showVC2() {
if let navController = self.navigationController{
navController.present(vc2, animated: true)
}
}

内部 vc2:

func returnFromVC2() {
if let navController = self.navigationController {
navController.popViewController(animated: true)
}else{
print("navigationController is nil") <-- I was reaching here!
}
}

正如“ stefandouganhyde”所说: “它不包含在你的 UINavigationController 或任何其他。”

新的解决方案:

func returnFromVC2() {
dismiss(animated: true, completion: nil)
}