How to navigate Inner view controller from App delegate - ios

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

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

Segue to a View Controller from my AppDelegate removes my UI Tab Bar

I am having a problem when performing segue from my AppDelegate. I am using this code to do a segue from appdelagate:
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let viewController: MyViewController = storyboard.instantiateViewController(withIdentifier: "myviewcontroller") as! MyViewController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.show(viewController, sender: self)
When I use this, my UITabBar is removed. I want to segue to a ViewController that is not a TabBar item and retain my UITabbar. I am also using navigation. How should I approach this?
To show the tab , you need to do the push from 1 of the tab vcs , and it should be embeded inside a navigation and use
let nav = self.window!.rootViewController as! UINavigationController
if let tab = nav.viewControllers.first as? UITabBarController ,
let innerNav = tab.viewControllers.first as? UINavigationController {
innerNav.pushViewController(viewController,animated:true)
}

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.

Tab Bar does not show on View Controller inside Navigation Controller

I'm running into this issue when I'm opening a new View Controller programmatically.
let controller = self.storyboard?.instantiateViewController(withIdentifier: "overViewScreen") as! OverviewViewController
controller.user = self.userObject
let navigationController = UINavigationController(rootViewController: controller)
self.present(navigationController, animated: true, completion: nil)
The structure of my project :
storyboard
On my storyboard the tab bar is shown onto the View Controller (with the table on the right), but when I run the app it looks like this :
enter image description here
I hope you guys can help me out!
Thank you.
You are presenting NavigationController without tab bar controller. You need to present TabBarController.
Give your TabBarController identifier and instantiate them just like you've done with controller
code from comment:
let tabVC = UIStoryboard(name: "NameOfYourStoryboard", bundle: Bundle.main).instantiateInitialViewController() as! UITabBarController
let navVc = tabVC.viewControllers.first as! UINavigationController
let vc = navVc.viewControllers.first as! OverviewViewController
vc.incorrectAuthorization = SettingsAuthorizationMethod.fingerprint
vc.user = self.userObject
present(navController, animated: true, completion: nil)
Ok, I managed to fix it like this :
let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
vc.user = self.userObject
let nvc = UINavigationController(rootViewController: vc)
self.present(nvc, animated: true, completion: nil)
I made a seperate controller class "TabBarController", and added a property "user" to this class. In my "OverViewController" I can get the property as follows :
let tabBar: TabBarController = tabBarController as! TabBarController
user = tabBar.user
Thanks for the help!

How to instantiate a navigation and tab controller from another view controller which is in segue(show detail)?

I've asked a question before and it works successfully thanks to the one of the answers How to instantiate a navigation controller from another view controller?, but I have come across a new problem which is , whenever i click a button from a show detail segue, it supposed to navigate to a normal tab with a navigation bar, but it did nothing.
Here's the storyboard
Here's the scenario
1) User click on a button on a firstViewController and it will segue to a thirdViewController which is in show detail
2) User click another button which will then supposed to go to a secondViewController with the codes below
Here's the code
in ThirdViewController
#IBAction func buttonTapped(sender: UIButton) {
guard let tabBarController = tabBarController else { return }
let navController = tabBarController.viewControllers![1] as! UINavigationController
let secondViewController = navController.topViewController as! SecondViewController
secondViewController.name = "My name is TDog"
tabBarController.selectedIndex = 1
}
What did i do wrong? Do i still need to instantiate?
You have to dismiss the actual ThirdViewController but, in this class, you dont know yet UITabViewController so a way to obtain it ( not to re-instantiate but to recall from memory) is to call the rootViewController of your window (it can be done in your project):
#IBAction func buttonTapped(sender: AnyObject) {
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let tabBarController = appDelegate.window!.rootViewController
if tabBarController is UITabBarController {
let tab = tabBarController as! UITabBarController
let navController = tab.viewControllers![1] as! UINavigationController
let secondViewController = navController.topViewController as! SecondViewController
secondViewController.nameString = "My name is TDog"
tab.selectedIndex = 1
}
}
You got to call
self.presentingViewController.dismissViewControllerAnimated(true, completion: nil)
after the following line
tabBarController.selectedIndex = 1

Resources