Cancel button does not execute action - swift

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)

Related

Xcode TabBar controller logout issue

I have a tab bar controller in my app. one of the tabs has a navigation Controller with a bar button. clicking the bar button segues to a tableViewController which has another button in it. The button segues to yet another TableViewController which includes a logout button.
#IBAction func logoutDidTap(_ sender: Any) {
try! FIRAuth.auth()?.signOut()
when I login to the app again and click on that tab, it takes me to the TableViewController with the logout button instead of the beginning of the tab. How can I fix this?
Since you have placed all the view controllers under the navigation controller, so you can easily pop them from the navigation stack when you are done logging out. Here's how to do it:-
#IBAction fund logoutDidTap(sender:Any){
try! FirAuth.auth()?.signout()
var viewControllers = navigationController?.viewControllers
viewControllers?.removeLast(2) // views to pop
navigationController?.setViewControllers(viewControllers!, animated: true)
}

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

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.

another way to pop to root viewController

I have navigationBar with this side menu
I and I want add Button when I pressed go to root view controller
I tried using all of this
presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
self.navigationController!.popToRootViewControllerAnimated(false)
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
self.navigationController!.popToViewController(viewControllers[viewControllers.count ], animated: true);
please if anyone can try download the example and tell me what the wrong and what I should do
this is my main storybord
I want to pop this viewcontrller or any numbers of viewControllrs above it using sideMenu class
side menuclass is table, so I use this method didSelectRowAtIndexPath
some cell if user press it, it should pop all views above main views
this way work well with push, but pop No

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)
}

What is the correct way to push a View Controller after a selection from a menu?

In my app I have a "home" screen from which I can present a hamburger menu (coded as a modal transition in Storyboard).
The menu is a UITableView where a user will select a row. The menu has a delegate method which calls the following function in the presenting screen (my "home" screen)
// Menu delegate method
func menuDidExit(menuVC:MenuVC) {
self.dismissViewControllerAnimated(true, completion: {
// After dismissal of menu, call the chosen option
if let selectedOption = menuVC.menuSelection {
self.performSegueWithIdentifier(menuVC.menuSelection?.rawValue, sender: self)
}
})
}
This function will both dismiss the menu view controller, and then (once that transition is complete), present a second view controller based upon the chosen menu option, using the "performSegueWithIdentifier" command.
The issue that I have is that while the menu dismiss works fine (the menu slides off screen gracefully) - there is no animation for the presentation of the next view controller - it simply appears on screen after the menu has been dismissed.
I can call the desired view controller via a button/segue, and all works well, however when it is part of the completion block above it fails to animate the transition. This leads me to believe that there is something fundamentally wrong with my approach - hence the question, what is the correct way to push a second view controller after handling the dismissal of the first.
Many thanks for any suggestions
I think you can use this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.dismiss(animated: true) {
//dismiss your menu here
}
}
Hope it can help you.
Please let me know if it not work.
Thanks