Can't push a view controller again after popping it - swift

I want to push a view controller onto the screen when a button is pressed. It works fine the first time, but when I go back and press the same button once again, it does not push the view controller again. The same problem persists when I present the view controller in fullscreen mode. But I don't face this problem when presenting the view controller in the default mode, I am able to present and dismiss the view controller as many times as I want. Someone please help me out with my problem.
[![enter image description here][3]][3]
[![enter image description here][4]][4]
[![enter image description here][5]][5]
import UIKit
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UINavigationController(rootViewController: LoginViewController())
window.makeKeyAndVisible()
self.window = window
return true
}
}
#objc func signUpButtonPressed() {
let vc = SignUpViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
#objc func loginButtonPressed() {
self.navigationController?.popViewController(animated: true)
}
func configureNavigationBar() {
navigationController?.navigationBar.backgroundColor = .systemBackground
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(createButtonPressed))
title = "Sign Up"
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: Extensions.standardFont, size: 25)!]
navigationItem.rightBarButtonItem?.tintColor = .systemBlue
}
Please ignore the scrollview in the Sign up window, it is a work in progress and does nothing at this point in time. The Login, done and create account buttons all do the same work which is to pop the signup view controller. I am able to push the signup view controller once and pop it. But if I try to press the sign up button again, the button wouldn't even respond.

Try this in you objc func to navigate to next view controller:
#objc func signUpButtonPressedTapped() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SignUpViewController") as! SignUpViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}

Related

not getting navigationController?.pushViewController to work but present does?

I do have a non-Storyboard, 100% coded UIViewController, UICollectionView and UICollectionViewCell - works perfect.
here's the code in question:
SceneDelegate
not sure if this is relevant, tho.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let myController = MyViewController(collectionViewLayout: layout)
window?.rootViewController = myController
window?.makeKeyAndVisible()
}
.
.
ViewController
very simple and straight forward...
class MyViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let data = loadOnboardingData()
.
.
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(MyPageCell.self, forCellWithReuseIdentifier: "cellId")
collectionView?.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView?.tag = myPageControl.currentPage
setupMyPageControl()
}
ViewControllerExtention
here's the problem: the pushViewController method just doesn't do anything but the modal present works like a charm and I'm not getting what's wrong and why:
extension MyViewController: MyPageCellDelegate {
.
.
func didTabOnActionButton(title: String) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
guard let homeViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else {
print("Coun't find controller")
return
}
navigationController?.pushViewController(homeViewController, animated: true) <- NO EFFECT
//present(homeViewController, animated: true, completion: nil) <- WORKS PERFECT!
}
MyPageCell
I set up the Delegate via protocol and it seems that's fine too
protocol MyPageCellDelegate {
func didTabOnActionButton(title: String)
}
class MyPageCell: UICollectionViewCell {
var delegate: MyPageCellDelegate?
let myActionButton: UIButton = {
let button = UIButton(type: .system)
return button
}()
myActionButton.addTarget(self, action: #selector(self.doAction), for: .touchUpInside)
.
.
#objc private func doAction(_ sende: Any) {
delegate?.didTabEndOnboardingActionButton(title: "end Onboarding")
}
so, any Idea what's wrong with:
navigationController?.pushViewController(homeViewController, animated: true)
EDIT --------------------------------------------------------------------
As pointed out by #Michcio this here: window?.rootViewController = UINavigationController(rootViewController: myController) works half way and as far as I understand it, I'm embedding myController into an UINavigationController which adds the Navigation Bar to the current and following controllers.
But that's not what I need!
What I need is a clean and simple one for the onboarding i.e. MyViewController and the HomeViewController should be one with a Tab- and Navigation Bar
Basically starting from scratch after onboarding.
I used to solve this in the previous version editing the AppDelegate first Method like this (in this example I used Storyboards):
extension AppDelegate {
func showOnboarding() {
if let window = UIApplication.shared.keyWindow, let onboardingViewController = UIStoryboard(name: "Onboarding", bundle: nil).instantiateInitialViewController() as? OnboardingViewController {
onboardingViewController.delegate = self
window.rootViewController = onboardingViewController
}
}
func hideOnboarding() {
if let window = UIApplication.shared.keyWindow, let mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() {
mainViewController.view.frame = window.bounds
UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: {
window.rootViewController = mainViewController
}, completion: nil)
}
}
}
and in the Delegate itself like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let isFirstRun = true // logic to determine goes here
if isFirstRun {
showOnboarding()
}
return true
}
but I'm seriously not getting the new SceneDelegate or simply don't understand it
Really would appreciate if someone could past some code here for re-use.
It didn't work, because you are set MyViewController as window.rootViewController. Just change line in SceneDelegate to:
window?.rootViewController = UINavigationController(rootViewController: myController)

AppDelegate not showing Storyboard buttons

I am setting up a MainViewController holding multiple ViewControllers the following way:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let pageController = UIPageViewController(
transitionStyle: UIPageViewControllerTransitionStyle.scroll,
navigationOrientation: UIPageViewControllerNavigationOrientation.horizontal,
options: nil
)
let navigationController = MainViewController(rootViewController: pageController)
navigationController.view.backgroundColor = UIColor.white
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let leftView = storyboard.instantiateViewController(withIdentifier: "VC1")
let middleView = storyboard.instantiateViewController(withIdentifier: "VC2")
let rightView = storyboard.instantiateViewController(withIdentifier: "VC3")
navigationController.viewControllerArray = [leftView, middleView, rightView]
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
return true
}
}
My MainViewController also has what is supposed to be a floating button. I have aded this in the storyboard (see picture below). The problem is that the button is not showing. How do i make it show?
I know that somehow this problem is related to the fact that I adding MainViewController in the AppDelegate, but I am not sure how that is hiding the buttons.
You aren't creating your MainViewController instance from the storyboard, you are just creating it with a call to its initialiser. This means that none of your storyboard elements will be loaded
You need something like:
let navigationController = storyboard.instantiateViewController(withIdentifier: "Main")
navigationController.viewControllers = [pageController]

Why during first boot my navigation bar is under status bar

Why when I create navigation controller with empty view controller I receive strange behaviour with navigation bar?
This is how I create navigation controller.
init(
window: UIWindow,
keystore: Keystore,
navigationController: UINavigationController = UINavigationController()
) {
self.navigationController = navigationController
self.keystore = keystore
super.init()
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
Than I simply do :
func start() {
let x = UIViewController()
x.view.backgroundColor = UIColor.red
navigationController.setViewControllers([x], animated: true)
}
And I receive:
But in the next app launches all is fine.
This might be the case, because you set the NavigationController after the app has already started.
I would recommend to set the NavigationController in your AppDelegate.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let firstViewController = UIViewController()
let navigationController = UINavigationController(rootViewController: firstViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
}

Self.revealViewController() returns nil

I am currently trying to get the pan gesture to reveal the controller. As of right now, my open button works, but when I add the last line, my program crashes because revealViewController is nil. Link to pod: https://github.com/John-Lluch/SWRevealViewController.
import Foundation
import UIKit
class MainViewController : UIViewController{
#IBOutlet weak var mainButton: UIView!
#IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated);
mainButton.makeCircle();
Open.target = self.revealViewController()
Open.action = #selector(SWRevealViewController.revealToggle(_:));
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());
}
}
It's likely you haven't set your SWRevealViewController to be the rootViewController. Remember to set Custom Class and Storyboard ID to be SWRevealViewController in your Identity inspector.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
self.view.window?.rootViewController = rootVC
// Cont. with your code...
}
In case you need to show a login page at the beginning, you need a function in you AppDelegate, which allows the window to update the rootViewController
func changeRootVCToSWRevealVC () {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
if let window = self.window{
window.rootViewController = rootVC
}
}
After user logged in, you can change the rooViewController to SWRevealViewController.
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.changeRootVCToSWRevealVC()
}
If you have a variable storing the state of the last log-in, and want to navigate directly to the Navigation Drawer menu rather than LoginViewController.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let rootVCId = isLoggedin ? "SWRevealViewController" : "LoginViewController"
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: rootVCId)
self.view.window?.rootViewController = rootVC
// Cont. with your code...
}
I was just having this same issue and came across this post. My issue turned out to be more of an "oops!" as I missed something obvious and spent 30 minutes scratching my head on it. :facepalm:
If the accepted answer doesn't work for you, check to make sure you connected your menu property to the storyboard with an outlet.
Swift:
#IBOutlet weak var menuButton:UIBarButtonItem!
override func viewDidLoad() {
if (self.revealViewController() != nil) {
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
}
}

how to create very first view for Swift 3

Hi I have made a ViewController.
But I don't know how to add very first view right after an app is tapped.
I added below codes in ViewController.swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "FirstViewController")
self.present(controller, animated: true, completion: nil)
the FirstViewController appears after the app tapped and shows white background for about a second.
When you first launch your app, the LaunchScreen.storyboard is displayed, as a transition while your app is loading. You can customise this in Interface Builder.
check the option 'is initial view Controller' for that firstViewController from the storyboard.
You can set initial view controller programmatically or via storyboard
Method 1:programmatically--
Set storyboard-identifire for Viewcontroller in Main.storyboard file
like
After that set rootviewController in Appdelegate class.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
window?.rootViewController = redViewController
window?.makeKeyAndVisible()
return true
}
}
For setting up with navigation controller use UINavigationController -
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryBoard.instantiateViewController(withIdentifier: "viewController") as! viewController
let navigationController = UINavigationController(rootViewController: viewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
Method 2:Via storyboard--
Go to respective storyboard & select Is Initial View Controller