Xcode TabBar controller logout issue - swift

I have a tab bar controller in my app. one of the tabs has a navigation Controller with a bar button. clicking the bar button segues to a tableViewController which has another button in it. The button segues to yet another TableViewController which includes a logout button.
#IBAction func logoutDidTap(_ sender: Any) {
try! FIRAuth.auth()?.signOut()
when I login to the app again and click on that tab, it takes me to the TableViewController with the logout button instead of the beginning of the tab. How can I fix this?

Since you have placed all the view controllers under the navigation controller, so you can easily pop them from the navigation stack when you are done logging out. Here's how to do it:-
#IBAction fund logoutDidTap(sender:Any){
try! FirAuth.auth()?.signout()
var viewControllers = navigationController?.viewControllers
viewControllers?.removeLast(2) // views to pop
navigationController?.setViewControllers(viewControllers!, animated: true)
}

Related

Switch tab bar controller view controller after dismissing a modally presented view controller

In my project you can create a post from a modal view.
When the modal view is dismissed (user presses on save post) I want to switch the tab bar controller to the second tab (post feed screen).
This topic is similar to my problem. The only difference being this is presented from a modal view. I can't figure out how to implement it in my code (tab bar is nil)
Switch tab bar programmatically in Swift
I have added 3 images to make this issue clearer
code screenshot
console message
#objc func saveAction(sender: UIButton) {
print ("> save pressed")
print(presentingViewController?.tabBarController)
print(presentingViewController)
presentingViewController?.tabBarController?.selectedIndex = 1
dismiss(animated: true)
}
edit: sorry stack overflow doesn't allow me to add images yet
You can do this using delegate pattern. But if you prefer not to add a delegate for this, you can do as shown below;
You can switch the tabbar by changing the selectedIndex property of tabBarController
if let presenter = presentingViewController as? LibraryViewController {
presenter.tabBarController?.selectedIndex = 1
}
dismiss(animated: true)
If you are presenting the modal on navigation controller in tabbar, use:
if let tabBar = presentingViewController as? UITabBarController {
tabBar.selectedIndex = 1
}
dismiss(animated: true)

Back Button Takes Me To Wrong View Controller

I have a storyboard I'm working with that is setup as so:
Login Screen -> Tab Bar Controller - > Navigation Controller - > Screen 1 Segue to Screen 2 Segue to Screen 3.
The first time I login, everything works great. Screen 1 segues to screen 2, screen 2 segues to screen 3, you can then use the back button to go back to screen 2 and then screen 1. However, I have a "logout" function (code below, although I don't think this is relevant to my issue) and after I "logout", it takes me to the Login Screen (first screen in the sequence above). When I then login again, navigate to Screen 2 or Screen 3, pressing the back button from the segue takes me all the way back to the login screen as opposed to the prior Screen 1 or Screen 2.
#objc func logOut(){
let homeView = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(homeView, animated: true)
homeView.navigationItem.hidesBackButton = true
}
I'm not sure that instantiating the LoginViewController again is the best practice, since you already have it in you navigation stack. I would recommend doing something like this:
#objc func logOut(){
self.navigationController?.popToRootViewControllerAnimated(true)
}
This will remove all the view controllers from the navigtion stack, and present you with a root view controller (LoginViewController)
Updated for anyone who is curious. The pushViewController option was the issue. If i just use "present", I no longer have this issue. Here is my updated logout code:
#objc func logOut(){
let homeView = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.present(homeView, animated: true)
homeView.navigationItem.hidesBackButton = true
}

Swift Trigger click in textfield

In my app I'm using some pickerviews that appear when I click on a textfield, that works fine!
I wanted to do the same when I click on a Left Bar Button Item. I can't do it with the button because buttons doesn't have inputView property, needed for associate the pickerview to the button (in this case). So I want to have a hidden textfield that is programmatically clicked when I click on the button (when it's clicked it show the pickerview and change the button name, that's all done)
Is that possible?
The best I can do right know is something like this
txtFantasma.perform(
#selector(becomeFirstResponder),
with: nil,
afterDelay: 0.1
)
It works fine, but just work at the first time.
EDIT1:
I've tried to make it with buttons... The popover is showing, now I wanted to click in one button and dismiss the popover and pass data to the main viewcontroller.
class ViewPopup:UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btTituloAsc(_ sender: UIButton) {
let next = self.storyboard?.instantiateViewController(withIdentifier: "mainview") as! ViewController
next.ordenacao = "TituloAsc"
self.present(next, animated: true,completion:nil)
}
}
This works, but the main controller is showed without the Navigation Bar! How can I do the same but show de Navigation Bar?
The simplistic way to do it, is just display the pickerView as a popover once the user taps the button and then change the buttons title accordingly.

Tab Bar Controller isHidden, but stays hidden

Im currently designing an app that utilizes a tab bar controller.
On the messages tab (instant messages), I want the tab bar to disappear whenever a user is having/viewing his/her conversation with another person. To do so I used this:self.tabBarController?.tabBar.isHidden = true
It disables the tabBar, but now the issue is that whenever I hit the back button to return to previous views (embedded in a navigation controller), the tab bar is still hidden. On the other views, I've set tabBar.isHidden = false, but that doesn't seem to fix it and now I can't access any of the other tabs.
My question is: How can I hide the tabBar on one view but keep it visible when I return to previous views?
In TabBar firstViewController
override func viewWillAppear(animated: Bool) {
// Enable TabBar
self.tabBarController?.tabBar.hidden = false
}
In SecondViewController (Pushed from firstViewController)
override func viewDidLoad() {
super.viewDidLoad()
// Disable TabBar
self.tabBarController?.tabBar.hidden = true
}

Tab bar disappears when returning back to the table view controller from another view

I have a table view controller connected to a tab bar controller and everything works fine. Then I have a simple view controller where the user can create a new post and I pass from the table view controller to this view with a button "new post" and then the user can either click "post" and get back to the table view or by pressing the "back" button. Everything works fine but when returning back from the view to the table view the tab bar disappears. I put the photo of the storyboard: the main.storyboard
Like what #vacawama suggested you should not be using a show segue but a unwind segue. However, you can also do it in code like this if your CustomViewControllerare in a UINavigationController
#IBAction func backButtonPressed(sender: UIButton) {
self.navigationController.popViewControllerAnimated(true)
}
Or in a presented modal like this :
#IBAction func backButtonPressed(sender: UIButton) {
self.dismissViewControllerAnimated(true, completion: nil)
}