UINavigationController shows RootViewController before showing topViewController - ios

I'm trying to show view controller which is 2nd view controller in a navigation view controller. What I want to show RegistraionViewController if join is tapped. Based on that. I push registrationViewController to the navigation Controller where LoginViewController is rootView Controller. Now the issue is when Join is Tapped. It Should show registrationView Controller but it shows Login screen and after a delay of about half second. It shows registration screen. My Code is as follows:
if let navC = self.selectedViewController as? UINavigationController
{
if let vc = navC.viewControllers.first as? LoginViewController
{
if let registerVC = self.storyboard?.instantiateViewControllerWithIdentifier("RegistrationViewControllerIdentifier") as? RegistrationViewController
{
var view = navC.view
vc.navigationController?.pushViewController(registerVC, animated: false)
}
}
}
I have also tried by changing the transition of views and giving animation time as 0. But no luck. Which is :
if let registerVC = self.storyboard?.instantiateViewControllerWithIdentifier("RegistrationViewControllerIdentifier") as? RegistrationViewController
{
UIView.animateWithDuration(0.0, animations: {
if let view = self.navigationController?.view
{
self.navigationController?.pushViewController(registerVC, animated: false)
UIView.setAnimationTransition(UIViewAnimationTransition.None, forView:view , cache: false)
}
}, completion: {
(value: Bool) in
})
}

Related

How to present a ViewController with a Navigationbar programatically?

When presenting my viewcontroller this way I don't get a navigationbar, even though the viewcontroller is embedded in a navigation controller in the interface builder.
if let stockVC = storyboard?.instantiateViewController(withIdentifier: "Stock") as? StockTableViewController {
stockVC.stockToDisplay = commonData.stock.filter { $0.productID.contains(product) }
// No NavigationBar with this one
navigationController?.present(stockVC, animated: true)
// No NavigationBar with this one
self.present(stockVC, animated: true)
}
I know the navigationBar works, because if I set the ViewController as initial in the storyboard, it shows.
What am I doing wrong here?
When you present a view controller, it is not part of the current navigation stack. You need to create a new navigation controller with your stockVC contained in it, and present the new navigation controller:
if let stockVC = storyboard?.instantiateViewController(withIdentifier: "Stock") as? StockTableViewController {
stockVC.stockToDisplay = commonData.stock.filter { $0.productID.contains(product) }
let stockNavigationController = UINavigationController(rootViewController: stockVC)
self.present(stockNavigationController, animated: true)
}

Failing to open the app in a specific view controller after taping in a local notification

I have this app with the fallowing View Hierarchy:
HOME
UINavigationController
MainViewController
UINavigationController
DashboardController
YTPageController
DashboardInitialViewController
USERS
UINavigationController
UsersViewController
ADD USERS
UINavigationController
AddUsersViewController
From HOME you can show segue to USERS, and from USERS you can show segue to ADD USERS
I want to be able to tap a local notification and go directly to ADD USERS, no matter where I am in the app.
If I'm in HOME, there is no problem.
The problem is where I'm in USERS and I tap in the notification. Nothing happens.
I have try to remove USERS FROM the View Hierarchy but nothing seems to happen.
This is the code that executes when I tap in the notification:
if let conversationVC = storyboard.instantiateViewController(withIdentifier: "AddUsersViewController") as? AddUsersViewController {
if var navController = self.window?.rootViewController as? UINavigationController {
navController.isNavigationBarHidden = true
navController.popToRootViewController(animated: true)
navController.pushViewController(conversationVC, animated: true)
}
}
I have try in the other navigation controller in HOME, but also nothing.
if let conversationVC = storyboard.instantiateViewController(withIdentifier: "AddUsersViewController") as? AddUsersViewController {
if var navController = self.window?.rootViewController as? UINavigationController {
if let main = navController.viewControllers[0] as? UIViewController {
if let nav = main.navigationController as? UINavigationController {
nav.popToRootViewController(animated: true)
nav.isNavigationBarHidden = true
nav.pushViewController(conversationVC, animated: true)
}
}
}
}
Set the animated to false or use the other function with completed and push the viewController in the completed function.
The problem is that the code directly keeps on running and tries to push the new view controller on the other one which isn't popped yet.
navController.isNavigationBarHidden = true
navController.popToRootViewController(animated: false)
navController.pushViewController(conversationVC, animated: true)
and...
nav.popToRootViewController(animated: false)
nav.isNavigationBarHidden = true
nav.pushViewController(conversationVC, animated: true)

how to prevent user from being able to call the same view controller twice in a row from a slide out settings menu?

I have the following function that identifies the navigation controller that is embedded in a tab bar controller and pushes a profile view controller. This function works, but I want to do some check that prevents it from presenting the profile view controller a second time if this function is called from the slide out menu while the profile view controller is the most recently pushed view controller. Here's the function:
private func toProfile() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let tbc = appDelegate.window?.rootViewController as? TabBarController,
let nav = tbc.viewControllers?[tbc.selectedIndex] as? UINavigationController else { return }
let profileVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "profileVC")
nav.pushViewController(profileVC, animated: true)
delegate?.dismissSettingsVC()
}
I tried:
if nav.viewControllers.last == profileVC {
print("Do nothing")
} else {
nav.pushViewController(profileVC, animated: true)
}
but it never says the two are equal. How do I make an if statement to check if the last view controller pushed is profileVC?
You need to check the type
if nav.viewControllers.last is ProfileVC {
print("Do nothing")
}
else {
nav.pushViewController(profileVC, animated: true)
}
Currently you compare 2 instances of the same type and for sure they are not equal

Dismissing a custom viewcontroller

I'm currently using a third party called "Form".
In my UIButton, I implement a method which initializes a custom view controller which is a subclass of FormViewController. I initialize FormViewController embedded in navigation controller.
In my FormViewController class, I tried below two methods but none of them did not work.
self.navigationController?.popViewControllerAnimated(true)
self.dismissViewControllerAnimated(true, completion: nil)
Codes for pressing a UIButton:
#IBAction func part1Pressed(sender: AnyObject) {
if let JSON = NSJSONSerialization.JSONObjectWithContentsOfFile("data.JSON") as? [String : AnyObject] {
let initialValues = [
"last_name" : "Nordman"]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewContainerVC = storyboard.instantiateViewControllerWithIdentifier("viewContainerVC")
let sampleController = Part1_VC(JSON: JSON, initialValues: initialValues)
let rootViewController = UINavigationController(rootViewController: sampleController)
rootViewController.view.tintColor = UIColor(hex: "5182AF")
rootViewController.navigationBarHidden = false
FORMDefaultStyle.applyStyle()
FORMSeparatorView.appearance().setSeparatorColor(UIColor.clearColor())
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = rootViewController
self.window?.makeKeyAndVisible()
}
}
Thank you!
You are trying to dismiss the view controller when it wasn't presented in the first place.
Instead set the root view controller of the navigation controller to something else (e.g. the screen you want shown after you dismiss the form) and present your form view controller modally:
self.presentViewController(formVc, animated: true, completion: nil)
You can then dismiss the view controller as you intended when you are finished with the form.

How to make View Controller Animated When called in App Delegate

I am dismissing the login view controller from the AppDelegate.swift file. but the view controller just vanishes off the screen when i run the below code, how can i have the view controller slide down.
var currentUser = PFUser.currentUser()
if currentUser != nil {
var dashVC = mainSB.instantiateViewControllerWithIdentifier("dashView") as! itsTimeDashVC
window!.rootViewController = dashVC
window!.makeKeyAndVisible()
}
Updating root view with animation, May be that should work. I do not have tried it :
UIView.transitionFromView(self.window.rootViewController.view, toView: viewController.view, duration:0.6, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil), completion: { (fininshed: Bool) -> () in
self.window.rootViewController = viewController
})

Resources