SWRevealViewController NOT working - ios

I simply implemented this tutorial until minute 8:35 , and it did not work.
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
print("open")
Open.target = self.revealViewController()
Open.action = Selector("revealToggle:")
print("open2")
}
override func viewDidAppear(_ animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}}
However, its worth to note. I am getting to this page after splash screen page.And below how am doing it
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Navigate") as! UINavigationController
//self.present(vc, animated: true, completion: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = vc
From Splashscontroller i am calling to go main.storyboard.
Navigate, I click on navigation controller and give it storyboardid Navigate.
I get to the page, with open button..but no right reveal controller opens when i click on it.

Please edit your appdelegate "didFinishLaunchingWithOptions" method's code as below
// create viewController code...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier(""Navigate") as! UINavigationController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("Left") as! LeftSideMenuViewController
let slideMenuController = SlideMenuController(SailDataViewController:mainViewController , LeftSideMenuViewController : leftViewController)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
please check in your class name , because I haven't run this code . But in my project it is working.

Related

Navigation Controller is not loading ( push , present ) another viewController

I cannot load the viewController on clicking the button, knowing that the defined Vc holds a value of the VC that i want to present.
//AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "ViewController") as! ViewController
let navVC = UINavigationController.init(rootViewController: mainViewController)
self.window?.rootViewController = navVC
self.window?.makeKeyAndVisible()
return true
}
//ViewController Class
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var button: UIButton!
#IBOutlet weak var pageController: UIPageControl!
private let reuseIdentifier = /*"CollectionViewCell"*/ "Cell"
let collectionViewImages : [UIImage] = [#imageLiteral(resourceName: "cooov"), #imageLiteral(resourceName: "shutterstock535739452"), #imageLiteral(resourceName: "portraitYoungManLyingBedCheckingHisFeverThermometer232147948490")]
let collectionViewTexts : [String] = [
"Stay at home ! your health is our Priority",
"Access Latest Coronavirus articles !",
"Check up on your health on a daily basis !"
]
override func viewDidLoad() {
super.viewDidLoad()
renderButton()
renderCell()
pageController.numberOfPages = collectionViewImages.count
pageController.currentPage = 0
}
#IBAction func buttonClicked(_ sender: UIButton) {
let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
as! SignUp
self.navigationController?.pushViewController(signupVC, animated: true)
}
}
//SignUp class The VC that should appear on ButtonClick
class SignUp : BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
//the class that is inherited by ViewController
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true
}
}
[enter image description here][1]}
enter image description here
here is a link to the pics provided please check.
https://drive.google.com/drive/folders/1CxysH911PIa3-S9Dx6EUV7DE8vlnEd4l?usp=sharing
Here is the function you can change it like this and let me know the outcome
first run this one
#IBAction func buttonClicked(_ sender: UIButton) {
let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
let navVC = UINavigationController.init(rootViewController: signupVC)
// self.present(navVC, animated: true, completion: nil)
if let nav = self.navigationController {
nav.pushViewController(navVC, animated: true)
} else {
print("navigation controller not found")
}
}
And then try to present ... and let me know what happened
#IBAction func buttonClicked(_ sender: UIButton) {
let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
let navVC = UINavigationController.init(rootViewController: signupVC)
self.present(navVC, animated: true, completion: nil)
}
You don't need new UINavigation controller to push.
#IBAction func buttonClicked(_ sender: UIButton) {
let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
as! SignUp
self.navigationController?.pushViewController(signupVC, animated: true)
}
Also if you are working with iOS 13.0+ and using Xcode 11+, your
SceneDelegate will be called instead of AppDelegate. So try copying the paste to SceneDelegate as well.

Loading a new viewController on the same tab bar item not working

So I am having a problem loading a new view controller on the same tab bar item. I have a tab bar controller as my root controller and 4 tabs on the tab bar. One of which is the account tab, when the user is logged in he would need to see an overview of his account details but when there is no user there needs to be a sign-in / login page on that tab bar item. For the moment I have a custom tab bar class for my tabbarcontroller with a function that I thought was the solution but nothing happens. I have put a breakpoint in the view controller that needs to loaded (the user detail page) and it comes in the ViewDidLoad so it loads but it doesn't appear on the screen.
Hope I can finally solve this problem!
Kind regards
B.
this is my custom tab bar controller class:
import UIKit
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
var window: UIWindow?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 3{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
window?.rootViewController = loginVC
window?.makeKeyAndVisible()
}
return true// you decide
}
}
here is the code that presents full screen loginVc
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 1{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: false, completion: nil)
}
return true// you decide
}
If you also want to have bottom bar ... your that viewController should be Navigation Controller and you can push your desired ViewController then like
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
let index = tabBarController.viewControllers?.firstIndex(of: viewController)
if index == 1{ // Index of Account tab
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = mainStoryBoard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewController
// loginVC.modalPresentationStyle = .fullScreen
if let navigation = viewController as? UINavigationController {
navigation.pushViewController(loginVC, animated: false)
}
}
return true// you decide
}
It will show back button and navigationBar on top
if you dont want NavigationBar then in your MessagesViewController did load function call
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
// Do any additional setup after loading the view.
}
And if you just want to hide back button then call
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: false);
// Do any additional setup after loading the view.
}
Changing your rootViewController is bad practice in Swift.
The correct way to handle tasks like this is to override your accountViewController's viewWillAppear function to determine if the user isn't logged in and present your loginViewController. Try something like:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// authManager would be a custom class that manages authorization with a property for loggedIn
if authManager.loggedIn == false {
guard let loginVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "login") as? LoginViewController else { return }
self.present(loginVC, animated: true, completion: nil)
}
}

Not navigate to specific view controller?

I am trying to navigate to specific view controller but it is not working.
what i am doing is below
var viewControllers:[UIViewController]!
var firstView : UIViewController!
let storyboard = UIStoryboard(name: "Main", bundle: nil)
firstView = storyboard.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
viewControllers = [firstView]
#IBAction func BackAction(_ sender: Any) {
for aViewController in viewControllers {
if aViewController is FirstViewController {
self.navigationController!.popToViewController(aViewController, animated: true)
}
}
}
but not working .
Please help me where i am wrong.
Thanks

How to dismiss the current ViewController and go to another View in Swift

I am new to Swift and I want to know how to dismiss the current view controller and go to another view.
My storyboard is like the following: MainMenuView -> GameViewController -> GameOverView. I want to dismiss the GameViewController to go to the GameOverView, not to the MainMenuView.
I use the following code in my MainMenuView:
#IBAction func StartButton(sender: UIButton) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameViewController") as! GameViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
restGame()
}
In the GameViewController, I use this code, but it doesn't dismiss the GameViewController.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameOverView") as! GameOverView
self.presentViewController(nextViewController, animated:true, completion:nil)
This is My GameOverView Code :
class GameOverView: UIViewController{
// save the presenting ViewController
var presentingViewController :UIViewController! = self.presentViewController
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func ReplayButton(sender: UIButton) {
restGame()
didPressClose()
}
#IBAction func ReturnMainMenu(sender: UIButton) {
Data.GameStarted = 1
self.dismissViewControllerAnimated(false) {
// go back to MainMenuView as the eyes of the user
self.presentingViewController.dismissViewControllerAnimated(false, completion: nil);
}
/* let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("MainScene") as! MainScene
self.presentViewController(nextViewController, animated:true, completion:nil)*/
}
func restGame(){
Data.score = 0
Data.GameHolder = 3
Data.GameStarted = 1
Data.PlayerLife = 3.0
Data.BonusHolder = 30
Data.BonusTimer = 0
}
func didPressClose()
{
self.self.dismissViewControllerAnimated(true, completion:nil)
}
override func shouldAutorotate() -> Bool {
return false
}
deinit{
print("GameOverView is being deInitialized.");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
Any suggestions?
What you can do is let the GameOverView be presented, after all when you presenting it the GameViewController is below in the hierarchy, and then in your GameOverView run the following code to close both when you want to dismiss the GameOverView, like in the following way:
#IBAction func ReturnMainMenu(sender: UIButton) {
// save the presenting ViewController
var presentingViewController: UIViewController! = self.presentingViewController
self.dismissViewControllerAnimated(false) {
// go back to MainMenuView as the eyes of the user
presentingViewController.dismissViewControllerAnimated(false, completion: nil)
}
}
The above code need to be called when you want to dismiss the GameOverView.
I hope this help you.
The below code will take you to the main VC, Here's a tried and tested piece of code.
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

Presenting view controller function not being called iOS Swift

For this problem, the view controller in which this code is run is in the Login.storyboard IB file. The view controller I am trying to present is in the Main.storyboard IB file. The code below is what I'm using to transition from one view controller to another.
func presentNextView() {
let mainSb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let screenAfterLogin: UIViewController = mainSb.instantiateViewControllerWithIdentifier("MainContainerView") as! UIViewController
self.presentViewController(screenAfterLogin, animated: true, completion: nil)
}
Running the application, I set 3 breakpoints on the 3 lines executed in the presentNextView function. Line one is called, line two is called, but line three is NOT called, and the next screen is not presented. Is there something wrong with xCode at the moment?
Checkout the project on github: https://github.com/joshuarcher/RecallBeta
Problem lies in file "HardLoginViewController.swift"
func presentNextView() {
let mainSb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let screenAfterLogin: SecondVC = mainSb.instantiateViewControllerWithIdentifier("SecondVC") as SecondVC
let navigationController = UINavigationController(rootViewController: screenAfterLogin)
presentViewController(navigationController, animated: true, completion: nil)
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
presentNextView()
}
func presentNextView() {
let mainSb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let screenAfterLogin: SecondVC = mainSb.instantiateViewControllerWithIdentifier("SecondVC") as SecondVC
let navigationController = UINavigationController(rootViewController: screenAfterLogin)
presentViewController(navigationController, animated: true, completion: nil)
}
}
import UIKit
class SecondVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title="Second Screen"
}
}

Resources