macOS - Transition between view controllers like iOS, dont open separate window - swift

Im building an app in swift for macOS, and I have a button on my initial view controller that I want to display a different view controller. I ctrl dragged from that button to the new view controller, and all of the segue options display the new view controller as a new window, rather that replacing the initial view. How can I make this button transition view controllers similar to how it works in iOS apps?

You can first make your segue to the 2nd view controller and then create an IBAction from your button where you call
self.dismiss(animated: true, completion: {})
within it. By doing this your initial view controller will no longer exist.
More detailed:
Segue from your button to the second view controller (ctrl drag)
Make an IBAction by ctrl drag from your button to the view controllers code
Put this into your IBAction:
self.dismiss(animated: true, completion: {})
Now it should look something like this:
#IBAction func dismissView(_ sender: Any) {
self.dismiss(animated: true, completion: {})
}
By clicking on your button your application go to the next view controller and the initial view controller will be dismissed.

Related

How to navigate from popover view controller in swift?

I have a button on a view controller and by clicking that button a view controller appears as a popup. That popup has a tableview and whenever I click on the tableview row I want it to navigate to different view controller. But I think it is not as simple as using this code.
let vc = storyboard?instantiateViewController(identifier: "controller") as! Controller
self.navigationController.pushViewController(popupView, animated: true, completion: nil)
Is there a way to open a view controller from popup view?

Swift dismissViewController from Segue - macOS

I have a secondary ViewController that appears programmatically via a Storyboard segue:
func actioncall ()
{
performSegue(withIdentifier: "showIgnoreVC", sender: self)
}
)
This function is part of the main ViewController, and is called via an NSNotification from the AppDelegate, which in turn is triggered by a Menu Item click.
However, even though the segue is connected to the main ViewController, the following code does not dismiss the secondary view:
#IBAction func dismiss(_ sender: Any)
{
print("Hello? Gonna close?")
self.presenting?.dismissViewController(self)
}
There are no errors, the function is called upon the correct Dismiss button click, but the secondary view does not dismiss. I have tried every variation of dismissViewController to no avail.
When I use a button on the Main view to activate the same segue, everything works as it is supposed to. I simply do not wish to clutter up the main View with a bunch of buttons.
Any ideas are appreciated, thank you very much.
dismissViewController works only for view controllers, that were presented modally (using Present Modally Segue)
As said in the answer to this SO question, you should dismiss view controllers, presented by Show Segue, like this:
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)

Tab bar disappears when returning back to the table view controller from another view

I have a table view controller connected to a tab bar controller and everything works fine. Then I have a simple view controller where the user can create a new post and I pass from the table view controller to this view with a button "new post" and then the user can either click "post" and get back to the table view or by pressing the "back" button. Everything works fine but when returning back from the view to the table view the tab bar disappears. I put the photo of the storyboard: the main.storyboard
Like what #vacawama suggested you should not be using a show segue but a unwind segue. However, you can also do it in code like this if your CustomViewControllerare in a UINavigationController
#IBAction func backButtonPressed(sender: UIButton) {
self.navigationController.popViewControllerAnimated(true)
}
Or in a presented modal like this :
#IBAction func backButtonPressed(sender: UIButton) {
self.dismissViewControllerAnimated(true, completion: nil)
}

Issue dismissing Popover presenting another ViewController

I've create a Popover segue in my project and inside a button to when it's touched open another ViewController modally. Then inside the another ViewController I've a button to dismiss the actual ViewController and the Popover too with the following code :
var tmpController :UIViewController! = self.presentingViewController;
self.dismissViewControllerAnimated(true, completion: {()->Void in
println("done");
tmpController.dismissViewControllerAnimated(true, completion: nil);
});
My problem is that when the actual ViewController is dismissed the Popover is like in FullScreen, then it resize to its original size and is dismissed after, and the resize process is animated.
What is causing this behaviour ?
How can I avoid this?
I would suggest you to dismiss the popover view controller from the parent view controller using delegation/notification.

Cancel button does not execute action

I created a project with six View over to the View menu that has a cancel button. if you are in one of the View from 2 to 6 and I open the menu, and then I press cancels hypothetically I return to View 1, how can I change the code so as to leave the current view? Also insert the code ....
#IBAction func unwindToMainViewController (sender: UIStoryboardSegue){
self.dismissViewControllerAnimated(true, completion: nil)
}
Thanks for aid.
UINavigationController has a popToRootViewControllerAnimated method to roll back to the root view controller.
If your view 1 is also the root view controller, and you are using a navigation controller, then you can use the following line of code:
self.navigationController?.popToRootViewControllerAnimated(true)