how to dismiss instantiated navigation rootviewcontroller in swift - ios

Below is my working code which I have used to instantiate my viewController
let myPage = self.storyboard?.instantiateViewControllerWithIdentifier("myViewController") as! myViewController
let myNav = UINavigationController(rootViewController: myPage)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = myNav
Could anyone help and provide a solution to dismiss myViewController?

Related

Removing the displayModeButton in UISplitViewController in iOS

I want to remove the displayModeButton from the detailViewController's Navigation Bar in splitveiwconroller. I tried setting leftBarButtionItem.isEnable = false in AppDelegate.swift file(as below)
if let splitViewController = self.window!.rootViewController as? UISplitViewController {
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem?.isEnabled = false
}
it hides the displaymodebutton intially when the application start. But when i swipt right to open the master view and then swipe left to dismiss the masterview controller the dispaly mode buttom reappars again. Please help me to hide the displayModeButton from detail view conroller.
Do the same in master viewController viewWillAppear:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController = controller
if let splitViewController = appDelegate.window!.rootViewController as? UISplitViewController {
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem?. isEnabled = false
navigationController.topViewController!.navigationItem.leftBarButtonItem?. tintColor = UIColor.clearColor()
}
OR if above does not work, use below code in AppDelegate
navigationController.topViewController!.navigationItem.setLeftBarButton(nil, animated: true)
Alternate to Daljeet's answer we can also use navigationItem.setHidesBackButton on the detailview controller or use this in the AppDelegate.swift's didFinishLaunchingWithOptions :
if let splitViewController = appDelegate.window!.rootViewController as? UISplitViewController {
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.setHidesBackButton(true, animated: true)
}

How to change a viewcontroller of a slidemenu?

I have 2 separate VC-s that each need to be displayed to the user depending on a state of the app. This is how I create my original slidemenu in AppDelegate, it works as intended:
let mainVC = MainNavVC.create()!
let leftMenuVC = LeftMenuVC.create()!
slideMenuController = SlideMenuController(mainViewController: mainVC, leftMenuViewController: leftMenuVC)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
But when I try to change the leftMenuVC the screen goes black, the menu button just stops working and tapGesture still shows the wrong menu. Here is a code I call to change the slidemenu:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.slideMenuController?.leftViewController = AnotherLeftMenuVC.create()!
appDelegate.window?.rootViewController = appDelegate.slideMenuController
I also tried an alternative approach to change the leftMenuVC:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.slideMenuController = SlideMenuController(mainViewController: MainNavVC.sharedInst!,
leftMenuViewController: AnotherLeftMenuVC.create()!)
appDelegate.window?.rootViewController = appDelegate.slideMenuController
appDelegate.window?.makeKeyAndVisible()
This will turn my screen black but tapGesture will at least show the correct leftMenu.
How to make this work ?
You can try this trick. I tested it in your library's sample project. This works but it's up to you to clean the code and make the unwrapping optionals and castings safer.
Basically the trick is to embed your leftController or rightController into a UINavigationController. And access such navigationController using the way you're trying to access the slideMenuController in the AppDelegate. And then replace the viewControllers of that navigationController.
In the viewDidLoad() of the MainViewController (I'm talking about the sample project, you should be familiar with this or relate your mainController here). I have this block of code that will be executed in 5 seconds ;) I use this approach when testing something, just to save time for the sake of demo.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerCellNib(DataTableViewCell.self)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
let appDel = UIApplication.shared.delegate as! AppDelegate
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rightViewController = storyboard.instantiateViewController(withIdentifier: "RightViewController") as! RightViewController
((appDel.window?.rootViewController as! ExSlideMenuController).leftViewController as! UINavigationController).viewControllers = [rightViewController]
}
}
and in the appDelegate, I embed the controller into a navigationController:
let slideMenuController = SlideMenuController(mainViewController:nvc, leftMenuViewController: UINavigationController(rootViewController: leftViewController), rightMenuViewController: rightViewController)
This should help you out.

How can we change rootview of navigationController? [duplicate]

This question already has answers here:
Set rootViewController of UINavigationController by method other than initWithRootViewController
(5 answers)
Closed 4 years ago.
Initially my app starts with a view controller(TabBarView) which I have set in AppDelegate file.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let tabBarView = TabBarView()
navigationController = UINavigationController(rootViewController: tabBarView)
window?.rootViewController = navigationController
Later in an app, when few criteria are met, I want to change my rootController to a new one(MusicPage).
At the moment I've create a new NavigationController object to display my (MusicPage)viewController however I think that my (TabBarView)viewController might be running in background and consuming memory.
I would really appreciate if someone tells me how to change my initial rootViewController.
Thanks. :)
The answer can be found on
Set rootViewController of UINavigationController by method other than initWithRootViewController
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav
Edited: SWIFT 4
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let yourVc = UIViewController() // your view controller
appDelegate.window?.rootViewController = yourVc
appDelegate.window?.makeKeyAndVisible()

App crash on pushViewController after popToViewController

I have tried to search for this, but can't find any perfect solution.
My app has custom animating views and both side drawers.I also have custom navigationController which has sliding top bar.From appDelegate I'm setting first controller as rootViewController and then pushing ViewController(i.e. mainViewController with animations).
then trying to push second controller. From second controller I can go to either third controller or rootViewController on button Click.
And app crashes when I tried to go to second viewController after getting back from secondController using popToViewController.
I'm setting rootViewController at appdelegate as:
let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "LoadingDataView") as! LoadingDataView
self.navigationController = SDNavigationController(rootViewController: profileViewController);
self.navigationController.navigationBar.isHidden=true;
self.window!.rootViewController = self.navigationController
self.window!.makeKeyAndVisible()
Pushing ViewController(mainController) as:
if( app.navigationController.viewControllers.count < 2){
let profileViewController=mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
app.navigationController?.pushViewController(profileViewController, animated: true);
}else{
let profileViewController=mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
app.navigationController?.pushViewController(profileViewController, animated: false);
let emptyView = UIViewController();
app.navigationController?.pushViewController(emptyView, animated: false);
app.navigationController.popViewController(animated: true);
}
Then pushing second viewController as:
let tutorialView=myStory.instantiateViewController(withIdentifier: "BookmarksDetails") as! BookmarksDetails;
self.navigationController!.pushViewController(tutorialView, animated: true);
getting back from second viewController as:
let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
testController.isOpenLeftDrawer = true
appDelegate.window?.rootViewController = testController
self.navigationController?.popToRootViewController(animated: true)
app crashes when I tried to go to secondViewController from viewController(mainController).
Why you are doing this -
app.navigationController.popViewController(animated: true);
Just only push and don't use the pop ViewController code here.

How to navigate Inner view controller from App delegate

in my app delegate this is how I navigate to my SWRevealViewController
let revealViewController = mainStoryboard.instantiateViewControllerWithIdentifier("RevealView") as? SWRevealViewController
self.window!.rootViewController = revealViewController
self.window?.makeKeyAndVisible()
I navigate to that inner view controller from the SWRevealViewController's FrontViewcontrollerlike this.
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Read") as! ReadViewController
secondViewController.title = self.selectedTitle
self.navigationController?.pushViewController(secondViewController, animated: true)
Now in AppDelegate when receive a push notification I want to navigate this ReadViewController. and when I click the back button it should come back to the FrontViewController just like it happens in normal way. How can I do this in my notification delegates in AppDelegate
Please help me.
Thanks
Try this :
let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("Read") as! ReadViewController
let frontViewController = mainStoryboard.instantiateViewControllerWithIdentifier("frontViewController") as! FrontViewController
let navController = UINavigationController()
navController.viewControllers = [yourVC,frontViewController]
self.window!.rootViewController = navController
self.window?.makeKeyAndVisible()

Resources