1

While clicking on the button , i am moving to another view controller using the following code.

var window: UIWindow?
window = UIWindow.init(frame: UIScreen.main.bounds)
window?.autoresizesSubviews = true
window?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let trackingViewController = LoginCameraViewController.init(screen: 
.main)
window?.rootViewController = trackingViewController
window?.addSubview((trackingViewController?.view)!)
window?.makeKeyAndVisible()
window?.layoutSubviews()

For every button click, a new window is added to the application.I want to remove the latest window added. The number of windows present in the application can be known by using following code.

let windowz = UIApplication.shared.windows
print("subviews",windowz)
James Z
  • 12,209
  • 10
  • 24
  • 44
Swift_prasad
  • 65
  • 1
  • 11

4 Answers4

1

I think you get the wrong concept of navigation in iOS. Window is like a root object in which ViewControllers appear. So probably the solution you're looking in a first place is UINavigationController.

Apple Documentation on Navigation

inokey
  • 5,434
  • 4
  • 21
  • 33
0

For iOS 13 i was able to do it this way

I created array which contains the window using which this new viewController is being presented,

var arrWindow = [UIWindow]()
arrWindow.append(yourNewWindow)

// Note: This will be stored as strong reference so need to remove it.

Also store your original window in variable

let originalWindow = yourOriginalWindow

// Note: Same goes for this as well ,this will be stored as strong reference so need to remove it.

At the time of removing there are many ways to do it but this was the most suited way for me,

 func removeAppendedWindow() {

     for window in arrWindow {
         if window != originalWindow {
              if let index = arrWindow.index(of: window) {
                  window.isHidden = true
                  arrWindow.remove(at: index)
              }
         }
     }
}
Niki
  • 1,566
  • 1
  • 19
  • 36
  • @CyberMew I was able to handle at least my situation the way i mentioned it above. :) – Niki Oct 16 '19 at 13:30
-2

In the below code windowz is normal array.

let windowz = UIApplication.shared.windows 

You can remove last by using

windowz.removeLast()
-3

You should use View Controller instead of adding windows and pop it instead where you are removing the window. Window is only one object for app and will contain the views. Please correct your understanding and use View controllers.

Imran S
  • 157
  • 2
  • 10