Swift 使用present创建时,防止ViewController在后台堆叠
我使用以下代码打开新的ViewController:Swift 使用present创建时,防止ViewController在后台堆叠,swift,xcode,uiviewcontroller,Swift,Xcode,Uiviewcontroller,我使用以下代码打开新的ViewController: // Get a random next post @IBAction func buttonNextPostTapped(_ sender: UIButton) { let postNumber = Int.random(in: 0 ..< postIds.count) let postId = postIds[postNumber] PostApi.shared.getPost(postId: postI
// Get a random next post
@IBAction func buttonNextPostTapped(_ sender: UIButton) {
let postNumber = Int.random(in: 0 ..< postIds.count)
let postId = postIds[postNumber]
PostApi.shared.getPost(postId: postId) { (post) in
let storyBoard : UIStoryboard = UIStoryboard(name: "MainApplication", bundle: nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "PostsViewController") as! PostsViewController
nextViewController.post = post
nextViewController.isFromRandom = true
self.present(nextViewController, animated: true, completion: {})
}
}
//随机获得下一篇文章
@iAction func按钮nextPostTapped(\发送方:UIButton){
让postNumber=Int.random(in:0..
此代码将打开具有不同数据的同一ViewController。但是,它可以工作,“旧”视图控制器将堆叠在后台。因此,如果我打开10个新的ViewController,背景中就有10个VC
如何呈现一个新的ViewController,并消除“旧”的ViewController?使用
UINavigationController
中的setViewController
功能是最好的方法
func setViewControllers(_ viewControllers: [UIViewController], animated: Bool)
您可以像从堆栈中删除任何一个控制器
if var navigationControllersArray:Array = (self.navigationController?.viewControllers) {
navigationControllersArray.remove(at: navigationControllersArray.count-2)
self.navigationController?.viewControllers = navigationControllersArray
}
不能关闭视图控制器,除非它是模态控制器