Cannot push a view controller after successful login - ios

I am trying to send it to another view on successful login so I am just testing my logic at this stage
let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("myMainView") as! mainController
self.presentViewController(viewController, animated: true, completion: nil)
I have given my tab bar controller the name of myMainView but again i am being presented with
Could not cast value of type 'UITabBarController' (0x1abc8c828) to 'apertureswift.mainController' (0x1000210a0).
2016-10-09 11:56:17.549751 apertureswift[544:55199] Could not cast value of type 'UITabBarController' (0x1abc8c828) to 'apertureswift.mainController' (0x1000210a0).

if you send to UITabbarController you should change mainConrtoller to UITabbarController and identifier . you must set identifier for UITabController
let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tabcontroller") as! UITabbarController
self.presentViewController(viewController, animated: true, completion: nil)

Related

iOS- use of undeclared type viewcontroller

I am trying to switch from one controller to another. But the error shows undeclared type.
I have import UIKit on the controller but that also didn't work.
let storyboard = UIStoryboard(name: "ParticularUserInfo", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "AssignedScorerViewController") as! AssignedScorerViewController
viewController.USERID = USERID
self.present(viewController, animated: true, completion: nil)
Please verify that your Storyboard name is "ParticularUserInfo"? Probably it could be "Main". Also verify that in your storyboard for that particular ViewController identifier is set as "AssignedScorerViewController".
to set Identifier, select your ViewController in storyboard and set Storyboard ID, according to my attached screenshot.

How do I pass some value from one view controller to UINavigationController in swift 4 without using segue?

I want to pass value from one UIviewController to UINavigationViewController without using segue in swift 4.
My sample code is following
let vc = storyboard?.instantiateViewController(withIdentifier: "MainMenuId") as! UINavigationViewController
vc.sendValue = String //String send to sendValue which present in UINavigationController
self.present(vc!, animated: true, completion: nil)
But when I try to send at at that time my app is crashed and get error
Could not cast value of type 'UINavigationController' to 'UIViewController'
So How do I pass value from above structure without using segue. Thx in advance ;)
You can use below code:
let vc = storyboard?.instantiateViewController(withIdentifier:"MainMenuId") as! UIViewController
let nav = UINavigationController(rootViewController: vc)
vc.sendValue = String
self.present(nav!, animated: true, completion: nil)

Switch programmatically from a viewcontroller to another

I'm trying to create an app which will switch programmatically from a View Controller to another. I've already tried to do that with this code:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "EndViwController") as! EndViewController
self.navigationController?.pushViewController(EndViewController, animated: true)
But it turns me this error:
[Cannot convert value of type 'EndViewController.Type' to expected
argument type 'UIViewController']
Also, when I try this other code:
let timeLineTableVC = EndViewController()
self.present(timeLineTableVC, animated: true, completion: nil)
It gives to me a black screen on the simulator and the EndViewController doesn't appear.
In the end, when I try this:
let storyboard = UIStoryboard(name: "EndViewController", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EndViewController")
self.present(controller, animated: true, completion: nil)
It gives me this error:
[libc++abi.dylib: terminating with uncaught exception of type
NSException (lldb)]
Your first bit of code doesn't work because EndViewController is a type. You should replace this with secondViewController that you declared above.
The second bit of code doesn't work because you are creating an 'empty' EndViewController type and not instantiating the storyboard view .
I'm guessing the third bit of code is failing because your storyboard isn't called "EndViewController" (unless you've renamed it). Try changing this to "Main" instead.

Attempt to present UINavigationController whose view

AppDelegate
initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController")as! UIViewController
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
Takes me to LoginViewController
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CardsNavController") as? UIViewController
self.presentViewController(vc!, animated: true, completion: nil)
I click on the login button takes me to CardsViewController
func goToProfile(button: UIBarButtonItem) {
pageController.goToPreviousVC()
}
Clicking on back button which is part of the UINavigationController runs goToProfile (above) and takes me to ViewController.swift because that's where the pageController is declared.
let pageController = ViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)
The error shows up here
func goToPreviousVC() {
//let currentControllers = self.navigationController?.viewControllers
if viewControllers.isEmpty {
setViewControllers([profileVC], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: nil)
pageController.presentViewController(profileVC, animated: true, completion: nil)
else {
let previousVC = pageViewController(self, viewControllerBeforeViewController: viewControllers[0] as! UIViewController)!
setViewControllers([previousVC], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: nil)
}
Error:
Warning: Attempt to present <UINavigationController: 0x15ce314e0> on <MyApp.ViewController: 0x15cd2c6f0> whose view is not in the window hierarchy!
Basically the flow is like this AppDelegate -> LoginViewController -> ViewController and I need to get to ProfileViewController.
ProfileView has ProfileNavController while CardsView has CardsNavController
ViewController has a storyboardID of pageController.
Both ProfileView and and CardsView are embedded within UINavigationControllers (hence the NavController extension).
The second time I run the app after a fresh install it works perfectly (all the controllers get loaded okay). Should I push viewControllers in AppDelegate?
I've checked your code using Xcode 7, which may not be ideal for resolving this issue because I had to covert your code to Swift 2.0, but here was what I found out.
ISSUE
First time opening the app, this block:
if currentUser() != nil {
initialViewController = pageController
}
else {
initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController
}
self.window?.rootViewController = initialViewController
Will initialize LoginViewController and make it the current window's rootViewController.
At this point there is no pageController initialized
When user taps on the button to go to the Profile screen, this method will be called
func goToProfile(button: UIBarButtonItem) {
pageController.goToPreviousVC()
}
At this point, pageController is initialized, and off course, there is NOTHING in the viewControllers array. Let's see what happen in the goToPreviousVC method:
Original method looks like this:
let nextVC = pageViewController(self, viewControllerAfterViewController: viewControllers[0] as UIViewController)!
setViewControllers([nextVC], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
One thing you can see obviously is: calling viewControllers[0] could give you a crash because viewControllers is an empty array.
If you use Swift 2.0, it doesn't even let you compile your code :)
SOLUTION
Let's go directly to the solution: Ensure that the pageController is available before trying to call it's viewControllers.
I blindly tried fixing you code in Swift 2.0 and found out that this method would work for you:
BEFORE: In LoginViewController.swift line 63
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CardsNavController") as? UIViewController
self.presentViewController(vc!, animated: true, completion: nil)
AFTER: Let's fix it like this
let navc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CardsNavController") as! UINavigationController
if let viewControllers = pageController.viewControllers where viewControllers.count == 0 {
pageController.setViewControllers([navc.viewControllers[0]], direction: .Forward, animated: false, completion: nil)
}
self.presentViewController(pageController, animated: true, completion: nil)
It's working well here and probably I don't need to show you how the screen transition should look like :)
In case you would like to have the fixed source code as well, please find it HERE. Basically I converted your code to Swift 2.0 and ignored unnecessary parts like Facebook authentication for faster investigation.
Good luck & Happy coding!

opening a tab bar controller on successful login swift

I am extremely new to swift and storyboards. I have an initial view set which presents the user with login or register options. on the success of my login web service, I am trying to open a tab bar. I am getting into the success of the webservice as I can see the response.
My code for attempting to load the tab bar is as folllows in my initial view controller:
func loadHomeScreen()
{
emailField.text = ""
passwordField.text = ""
self.presentViewController(UIStoryboard.tabbarController()!, animated: true, completion: nil)
}
And at the very bottom of that file, I have the following:
private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
class func tabbarController() -> TabbarController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("TabbarControllerID") as? TabbarController
}
}
And in my storyboard I have given the tabbarcontroller the id above. When I run the app (tested on the simulator for iphone6), I am getting the error 'found nil while unwrapping an Optional value' and this is pointing to the line of code in my loadHome func above (self.presentViewController(UIStoryboard.tabbarController()!, animated: true, completion: nil))
Any help would be appreciated
You could instead instantiate your storyboard like so and the code would look like this under loadHomeScreen():
emailField.text = ""
passwordField.text = ""
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("TabbarControllerID") as! TabbarController
self.presentViewController(vc, animated: true, completion: nil)
You may need to change the bundle to the "mainBundle" as you have in your code, but this should work.
This may not be the optimal solution if your plan is to extend UIStoryboard for instantiating your ViewControllers but I think this might be a little easier/cleaner, depending on how your project is set up.

Resources