Swift Trigger click in textfield - swift

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.

Related

Button not click behind Navigationbar in swift 3

I use a button over a view behind the navigation bar by code and unselect the property UnderTopBars in storyboard due to which I have set the y coordinate = -45.when my navigation bar slide above than the button show up but its not clickable .Not fire the touchup inside method
my code is
searchBtn.addTarget(self, action: #selector(ViewController.searchAction(_:)), for: .touchUpInside)
func searchAction(_: UIButton) {
print("Button Tapped")
}
my button look like after hide the navigation Bar
you can use debug view hierarchy for if any view is on top of your button on not???
and also make sure your button is enabled.

Xcode TabBar controller logout issue

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)
}

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)
}

Swift - Reload view with new data programmatically

I have a MainView with some buttons. If a user for example presses button 1, I do navigate to a DetailView through a segue and where I programmatically add items to the view. The user can then press next, what I would like to do when this happens is to basically clear the DetailView and programmatically add new items that are customized to button 2 in the DetailView and when the user presses next clear DetailView again and programmatically add new items that are customized to button 3 etc...
From the MainView with the buttons I use this segue to navigate to the DetailView
func btnNext_Clicked(sender:UIButton){
self.performSegueWithIdentifier("showDetailView", sender: buttonId)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetailView" {
let detailViewController = segue.destinationViewController as! DetailViewController
detailViewController.buttonId = buttonId!
}
}
But now I´m not sure how to clear the detailView and add new items when the user presses "Next". Anyone have any idea how to do this?
If you want just to remove old buttons, and create new programmatically, feel free to use something like
view.subviews.map({ $0.removeFromSuperview() })
This will remove all subviews and you will be able to create new.

What is the correct way to push a View Controller after a selection from a menu?

In my app I have a "home" screen from which I can present a hamburger menu (coded as a modal transition in Storyboard).
The menu is a UITableView where a user will select a row. The menu has a delegate method which calls the following function in the presenting screen (my "home" screen)
// Menu delegate method
func menuDidExit(menuVC:MenuVC) {
self.dismissViewControllerAnimated(true, completion: {
// After dismissal of menu, call the chosen option
if let selectedOption = menuVC.menuSelection {
self.performSegueWithIdentifier(menuVC.menuSelection?.rawValue, sender: self)
}
})
}
This function will both dismiss the menu view controller, and then (once that transition is complete), present a second view controller based upon the chosen menu option, using the "performSegueWithIdentifier" command.
The issue that I have is that while the menu dismiss works fine (the menu slides off screen gracefully) - there is no animation for the presentation of the next view controller - it simply appears on screen after the menu has been dismissed.
I can call the desired view controller via a button/segue, and all works well, however when it is part of the completion block above it fails to animate the transition. This leads me to believe that there is something fundamentally wrong with my approach - hence the question, what is the correct way to push a second view controller after handling the dismissal of the first.
Many thanks for any suggestions
I think you can use this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.dismiss(animated: true) {
//dismiss your menu here
}
}
Hope it can help you.
Please let me know if it not work.
Thanks