How to open multiple windows one by one using a loop in OSX?

I can open multiple windows using a loop. Now I want to open these windows one by one. That means when I close one window the next window will be opened, I mean till the close of the first window next window should not be opened. Using below code all windows are getting opened at a time.

let selectedFiles = ["file1","file2"] 
for eachfile in selectedFiles 
{ 
let storyBoard : NSStoryboard = NSStoryboard(name: "Main", bundle:nil)
let newViewController = storyBoard.instantiateController(withIdentifier: "MainView") as? ViewController  
if newViewController != nil 
{ 
 newViewController.ParentView = self 
 self.presentAsModalWindow(newViewController!)  
} 
}

I want the second window to open only after closing first window

Did you try to create a global Bool

var oneWindowOpened = false


then set to true each time you open a window and set to false when you close.


Use it in your loop:


if newViewController != nil  && !oneWindowOpened {  
     oneWindowOpened = true
     newViewController.ParentView =  self.presentAsModalWindow(newViewController!) 
}

Did this make it work ? If not, please explain what happens. If yes, thanks to close the thread.

No this didnt worked, for loop will not wait till window closes and it is iterating and coming out. I used while loop and it is blocking the execution. I am looking for standard way or any cocoa API to fix this. Thanks

No this didnt worked, for loop will not wait till window closes and it is iterating and coming out.

You did not tell you wanted to control the timing as well.


I used while loop and it is blocking the execution.

Could you show what you did, that will make it easier to understand


I would try to execute in another thread. Take care, I did not test yet.


DispatchQueue.global().async {
     if newViewController != nil  && !oneWindowOpened {  
          oneWindowOpened = true
          DispatchQueue.main.async { () -> Void in
               self.newViewController.ParentView =  self.presentAsModalWindow(newViewController!)
         }
     }
}


If that does not work properly, you could introduce some waiting at line 4:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.001)     // wait 1ms. May have to adjust

Did that fix your problem ?


If not, please explain.


If yes, thanks to close the thread.

How to open multiple windows one by one using a loop in OSX?
 
 
Q